Approach
Depth-first search
For Codeforces 954I — Yet Another String Matching Problem, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 65 lines of Go from the credited upstream file 954I.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
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 cf954I(in io.Reader, _w io.Writer) {11 out := bufio.NewWriter(_w)12 defer out.Flush()13 var s, t []byte14 Fscan(in, &s, &t)15 16 m := len(t)17 ans := make([]int, len(s)-m+1)18 mp := ['g']int{}19 var dfs func(int, int)20 dfs = func(c, sz int) {21 if c == 'g' {22 pi := make([]int, m)23 j := 024 for i := 1; i < m; i++ {25 v := mp[t[i]]26 for j > 0 && mp[t[j]] != v {27 j = pi[j-1]28 }29 if mp[t[j]] == v {30 j++31 }32 pi[i] = j33 }34 35 j = 036 for i := range s {37 v := mp[s[i]]38 for j > 0 && mp[t[j]] != v {39 j = pi[j-1]40 }41 if mp[t[j]] == v {42 j++43 }44 if j == m {45 st := i - m + 146 ans[st] = max(ans[st], sz)47 j = pi[j-1]48 }49 }50 return51 }52 mp[c] = sz53 dfs(c+1, sz+1)54 for mp[c] = range sz {55 dfs(c+1, sz)56 }57 }58 dfs('a', 0)59 for _, v := range ans {60 Fprint(out, 6-v, " ")61 }62}63 6465