- 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
- 66 lines of Go from the credited upstream file 514C.go.
- The implementation keeps its working state in language-native values and containers.
- 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 910 1112type node14 struct {13 son [3]*node1414 end bool15}16 17func (o *node14) find(s string, miss bool) bool {18 if s == "" {19 return miss && o.end20 }21 for i, c := range o.son {22 if c != nil && (byte(i) == s[0]-'a' && c.find(s[1:], miss) || !miss && byte(i) != s[0]-'a' && c.find(s[1:], true)) {23 return true24 }25 }26 return false27}28 29type trie14 struct{ root *node14 }30 31func (t *trie14) put(s string) *node14 {32 o := t.root33 for i := range s {34 b := s[i] - 'a'35 if o.son[b] == nil {36 o.son[b] = &node14{}37 }38 o = o.son[b]39 }40 o.end = true41 return o42}43 44func CF514C(_r io.Reader, _w io.Writer) {45 in := bufio.NewReader(_r)46 out := bufio.NewWriter(_w)47 defer out.Flush()48 49 t := &trie14{&node14{}}50 var n, m int51 var s string52 for Fscan(in, &n, &m); n > 0; n-- {53 Fscan(in, &s)54 t.put(s)55 }56 for ; m > 0; m-- {57 if Fscan(in, &s); t.root.find(s, false) {58 Fprintln(out, "YES")59 } else {60 Fprintln(out, "NO")61 }62 }63}64 6566