- 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
- 88 lines of Go from the credited upstream file 1553B.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 CF1553B(_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 b17 }18 return a19 }20 21 var T int22 var txt, tar []byte23o:24 for Fscan(in, &T); T > 0; T-- {25 Fscan(in, &txt, &tar)26 n := len(tar)27 halfLen := make([]int, n)28 for i, mid, r := 0, 0, 0; i < n; i++ {29 hl := 130 if i < r {31 hl = min(halfLen[mid*2-i], r-i)32 }33 for ; i >= hl && i+hl < n && tar[i-hl] == tar[i+hl]; hl++ {34 }35 if i+hl > r {36 mid, r = i, i+hl37 }38 halfLen[i] = hl39 }40 isP := func(l, r int) bool { return halfLen[(l+r)/2]*2 > r-l+1 }41 42 maxMatchPrefix := func(text, pattern []byte) int {43 match := make([]int, len(pattern))44 for i, c := 1, 0; i < len(pattern); i++ {45 v := pattern[i]46 for c > 0 && pattern[c] != v {47 c = match[c-1]48 }49 if pattern[c] == v {50 c++51 }52 match[i] = c53 }54 lenP := len(pattern)55 c, mx := 0, 056 for _, v := range text {57 for c > 0 && pattern[c] != v {58 c = match[c-1]59 }60 if pattern[c] == v {61 if c++; c > mx {62 mx = c63 }64 }65 if c == lenP {66 return lenP67 }68 }69 return mx70 }71 72 preMax := maxMatchPrefix(txt, tar)73 for i := 0; i < n/2; i++ {74 tar[i], tar[n-1-i] = tar[n-1-i], tar[i]75 }76 sufMax := maxMatchPrefix(txt, tar)77 for i := 0; i < n; i += 2 {78 if isP(0, i) && n-i/2 <= sufMax || isP(n-1-i, n-1) && n-i/2 <= preMax {79 Fprintln(out, "YES")80 continue o81 }82 }83 Fprintln(out, "NO")84 }85}86 8788