- 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
- 71 lines of Go from the credited upstream file 633C.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 CF633C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 type node struct {15 son [26]*node16 s []byte17 }18 19 var n, m int20 var s, t []byte21 root := &node{}22 for Fscan(in, &n, &s, &m); m > 0; m-- {23 Fscan(in, &t)24 o := root25 for i := len(t) - 1; i >= 0; i-- {26 c := t[i]27 if c < 'a' {28 c += 3229 }30 c -= 'a'31 if o.son[c] == nil {32 o.son[c] = &node{}33 }34 o = o.son[c]35 }36 o.s = t37 }38 39 ans := [][]byte{}40 dp := make([]int8, n)41 var f func(int) int842 f = func(p int) (res int8) {43 if p == n {44 return 145 }46 dv := &dp[p]47 if *dv != 0 {48 return *dv49 }50 defer func() { *dv = res }()51 o := root52 for i := p; i < len(s); i++ {53 o = o.son[s[i]-'a']54 if o == nil {55 return -156 }57 if o.s != nil && f(i+1) == 1 {58 ans = append(ans, o.s)59 return 160 }61 }62 return -163 }64 f(0)65 for i := len(ans) - 1; i >= 0; i-- {66 Fprint(out, string(ans[i]), " ")67 }68}69 7071