- 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
- 73 lines of Go from the credited upstream file 1469E.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 "math/bits"8 "strconv"9 "strings"10)11 1213func CF1469E(_r io.Reader, _w io.Writer) {14 in := bufio.NewReader(_r)15 out := bufio.NewWriter(_w)16 defer out.Flush()17 18 var T, n, k int19 var s string20o:21 for Fscan(in, &T); T > 0; T-- {22 Fscan(in, &n, &k, &s)23 if n == 1 {24 Fprintln(out, "YES")25 Fprintln(out, s[0]&1)26 continue27 }28 k2 := bits.Len(uint(n)) - 129 left := k - k230 if left <= 0 {31 has := make([]bool, 1<<k)32 mask := 1<<(k-1) - 133 v, _ := strconv.ParseUint(s[:k-1], 2, 64)34 x := int(v)35 for _, c := range s[k-1:] {36 x = x<<1 | int(c&1)37 has[x] = true38 x &= mask39 }40 for i := 1<<k - 1; i >= 0; i-- {41 if !has[i] {42 Fprintf(out, "YES\n%0*b\n", k, 1<<k-1^i)43 continue o44 }45 }46 Fprintln(out, "NO")47 } else {48 has := make([]bool, 1<<k2)49 mask := 1<<(k2-1) - 150 c1 := strings.Count(s[:left-1], "1")51 v, _ := strconv.ParseUint(s[left:left+k2-1], 2, 64)52 x := int(v)53 for i := left - 1; i+k2 < n; i++ {54 c1 += int(s[i] & 1)55 x = x<<1 | int(s[i+k2]&1)56 if c1 == left {57 has[x] = true58 }59 x &= mask60 c1 -= int(s[i-left+1] & 1)61 }62 for i := 1<<k2 - 1; ; i-- {63 if !has[i] {64 Fprintf(out, "YES\n%s%0*b\n", strings.Repeat("0", left), k2, 1<<k2-1^i)65 break66 }67 }68 }69 }70}71 7273