- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 61 lines of Go from the credited upstream file 1295C.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 CF1295C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 var t int16 for Fscan(in, &t); t > 0; t-- {17 var s, t []byte18 Fscan(in, &s, &t)19 pos := [26][]int{}20 for i := range s {21 s[i] -= 'a'22 pos[s[i]] = append(pos[s[i]], i)23 }24 for i := range pos {25 pos[i] = append(pos[i], -1)26 }27 next := make([][]int, len(s)+1)28 next[0] = make([]int, 26)29 for i := range next[0] {30 next[0][i] = pos[i][0]31 }32 cur := [26]int{}33 for i, b := range s {34 cur[b]++35 next[i+1] = make([]int, 26)36 copy(next[i+1], next[i])37 next[i+1][b] = pos[b][cur[b]]38 }39 ans := 040 for i := 0; i < len(t); {41 si := next[0][t[i]-'a']42 if si == -1 {43 ans = -144 break45 }46 for i++; i < len(t); i++ {47 si = next[si+1][t[i]-'a']48 if si == -1 {49 break50 }51 }52 ans++53 }54 Fprintln(out, ans)55 }56}57 58596061