- 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
- 76 lines of Go from the credited upstream file 1679E.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 "strings"9)10 1112func cf1679E(in io.Reader, _w io.Writer) {13 out := bufio.NewWriter(_w)14 defer out.Flush()15 const mod = 99824435316 var n, q int17 var s string18 Fscan(in, &n, &s, &q)19 cq := strings.Count(s, "?")20 21 const mx = 1722 pow := make([][mx + 1]int, cq+1)23 for sz := 1; sz <= mx; sz++ {24 pow[0][sz] = 125 for j := 1; j <= cq; j++ {26 pow[j][sz] = pow[j-1][sz] * sz % mod27 }28 }29 30 f := [1 << mx][mx + 1]int{}31 for i := range 2*n - 1 {32 c := cq33 must := 034 l, r := i/2, (i+1)/235 for l >= 0 && r < n {36 if s[l] == '?' || s[r] == '?' {37 if l < r {38 c--39 }40 if s[l] != '?' {41 must |= 1 << (s[l] - 'a')42 } else if s[r] != '?' {43 must |= 1 << (s[r] - 'a')44 }45 } else if s[l] != s[r] {46 break47 }48 for sz := bits.OnesCount(uint(must)); sz <= mx; sz++ {49 f[must][sz] = (f[must][sz] + pow[c][sz]) % mod50 }51 l--52 r++53 }54 }55 56 for i := range mx {57 for s := 0; s < 1<<mx; s++ {58 s |= 1 << i59 for sz := bits.OnesCount(uint(s)); sz <= mx; sz++ {60 f[s][sz] = (f[s][sz] + f[s^1<<i][sz]) % mod61 }62 }63 }64 65 for range q {66 Fscan(in, &s)67 m := 068 for _, b := range s {69 m |= 1 << (b - 'a')70 }71 Fprintln(out, f[m][bits.OnesCount(uint(m))])72 }73}74 7576