- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 74 lines of Go from the credited upstream file 1149B.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF1149B(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 min := func(a, b int) int {15 if a < b {16 return a17 }18 return b19 }20 21 var n, q, id int22 var text, op, ch []byte23 Fscan(in, &n, &q, &text)24 nxt := make([][26]int, n+3)25 for i := 0; i < 26; i++ {26 nxt[n+1][i] = n + 227 nxt[n+2][i] = n + 228 }29 for i := n; i > 0; i-- {30 for j := 0; j < 26; j++ {31 nxt[i][j] = nxt[i+1][j]32 }33 nxt[i][text[i-1]-'a'] = i34 }35 36 s := [3][]byte{{0}, {0}, {0}}37 dp := [251][251][251]int{}38 for ; q > 0; q-- {39 Fscan(in, &op, &id)40 id--41 if op[0] == '+' {42 Fscan(in, &ch)43 s[id] = append(s[id], ch[0]-'a')44 st := [3]int{}45 st[id] = len(s[id]) - 146 for i := st[0]; i < len(s[0]); i++ {47 for j := st[1]; j < len(s[1]); j++ {48 for k := st[2]; k < len(s[2]); k++ {49 dp[i][j][k] = n + 150 if i > 0 {51 dp[i][j][k] = min(dp[i][j][k], nxt[dp[i-1][j][k]+1][s[0][i]])52 }53 if j > 0 {54 dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j-1][k]+1][s[1][j]])55 }56 if k > 0 {57 dp[i][j][k] = min(dp[i][j][k], nxt[dp[i][j][k-1]+1][s[2][k]])58 }59 }60 }61 }62 } else {63 s[id] = s[id][:len(s[id])-1]64 }65 if dp[len(s[0])-1][len(s[1])-1][len(s[2])-1] <= n {66 Fprintln(out, "YES")67 } else {68 Fprintln(out, "NO")69 }70 }71}72 7374