- 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
- 148 lines of Go from the credited upstream file 163E.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 "strconv"8)9 1011type acamNode63 struct {12 son [26]uint3213 fail uint3214}15 16var acamNodes63 [1e6 + 1]acamNode6317 18type acam63 struct {19 root uint3220 nodeCnt uint3221 g [][]uint3222}23 24func (t *acam63) put(s string) uint32 {25 o := t.root26 for _, b := range s {27 b -= 'a'28 if acamNodes63[o].son[b] == 0 {29 acamNodes63[o].son[b] = t.nodeCnt30 t.nodeCnt++31 }32 o = acamNodes63[o].son[b]33 }34 return o35}36 37func (t *acam63) buildFail() {38 t.g = make([][]uint32, t.nodeCnt)39 q := make([]uint32, 0, t.nodeCnt)40 for _, son := range acamNodes63[t.root].son[:] {41 if son != 0 {42 t.g[acamNodes63[son].fail] = append(t.g[acamNodes63[son].fail], son)43 q = append(q, son)44 }45 }46 for len(q) > 0 {47 o := q[0]48 q = q[1:]49 f := acamNodes63[o].fail50 for i, son := range acamNodes63[o].son[:] {51 if son == 0 {52 acamNodes63[o].son[i] = acamNodes63[f].son[i]53 continue54 }55 acamNodes63[son].fail = acamNodes63[f].son[i]56 t.g[acamNodes63[son].fail] = append(t.g[acamNodes63[son].fail], son)57 q = append(q, son)58 }59 }60}61 62type fenwick63 []int3263 64func (f fenwick63) update(i, j uint32, val int32) {65 for ; i < uint32(len(f)); i += i & -i {66 f[i] += val67 }68 for ; j < uint32(len(f)); j += j & -j {69 f[j] -= val70 }71}72 73func (f fenwick63) pre(i uint32) (res int) {74 for ; i > 0; i &= i - 1 {75 res += int(f[i])76 }77 return78}79 80func CF163E(_r io.Reader, _w io.Writer) {81 in := bufio.NewReader(_r)82 out := bufio.NewWriter(_w)83 defer out.Flush()84 85 var q, n int86 var s string87 Fscan(in, &q, &n)88 t := &acam63{nodeCnt: 1}89 nodeIDs := make([]uint32, n+1)90 for i := 1; i <= n; i++ {91 Fscan(in, &s)92 nodeIDs[i] = t.put(s)93 }94 t.buildFail()95 96 g := t.g97 nodes := make([]struct{ l, r uint32 }, len(g))98 type pair struct{ v, i uint32 }99 100 101 st := []pair{{t.root, 0}}102 nodes[t.root].l = 1103 dfn := uint32(1)104 for len(st) > 0 {105 p := st[len(st)-1]106 v, i := p.v, p.i107 if i < uint32(len(g[v])) {108 dfn++109 w := g[v][i]110 nodes[w].l = dfn111 st[len(st)-1].i++112 st = append(st, pair{w, 0})113 } else {114 nodes[v].r = dfn + 1115 st = st[:len(st)-1]116 }117 }118 119 bit := make(fenwick63, dfn+2)120 for i := 1; i <= n; i++ {121 p := nodes[nodeIDs[i]]122 bit.update(p.l, p.r, 1)123 }124 del := make([]bool, n+1)125 for ; q > 0; q-- {126 Fscan(in, &s)127 if s[0] == '?' {128 o := t.root129 ans := 0130 for _, b := range s[1:] {131 o = acamNodes63[o].son[b-'a']132 ans += bit.pre(nodes[o].l)133 }134 Fprintln(out, ans)135 } else {136 i, _ := strconv.Atoi(s[1:])137 if del[i] == (s[0] == '-') {138 continue139 }140 del[i] = !del[i]141 p := nodes[nodeIDs[i]]142 bit.update(p.l, p.r, int32(s[0]&3)-2)143 }144 }145}146 147148