- 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
- 62 lines of Go from the credited upstream file 1845C.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 . "fmt"5 "io"6 "slices"7)8 910func cf1845C1(in io.Reader, out io.Writer) {11 var T int12 var s, l, r string13o:14 for Fscan(in, &T); T > 0; T-- {15 Fscan(in, &s, &l, &l, &r)16 n := len(s)17 nxt := make([][10]int, n+1)18 for j := range nxt[n] {19 nxt[n][j] = n20 }21 for i := n - 1; i >= 0; i-- {22 nxt[i] = nxt[i+1]23 nxt[i][s[i]-'0'] = i24 }25 26 cur := -127 for i, b := range l {28 cur = slices.Max(nxt[cur+1][b-'0' : r[i]-'0'+1])29 if cur >= n {30 Fprintln(out, "YES")31 continue o32 }33 }34 Fprintln(out, "NO")35 }36}37 38func cf1845C(in io.Reader, out io.Writer) {39 var T, m int40 var s, l, r string41o:42 for Fscan(in, &T); T > 0; T-- {43 Fscan(in, &s, &m, &l, &r)44 vis, j := 0, 045 for _, b := range s {46 vis |= 1 << (b - '0')47 msk := 1<<(r[j]-l[j]+1) - 148 if vis>>(l[j]-'0')&msk == msk {49 j++50 if j == m {51 Fprintln(out, "NO")52 continue o53 }54 vis = 055 }56 }57 Fprintln(out, "YES")58 }59}60 6162