版本比较
AC自动机(原文)
正文
增加 228 行删除 227 行
55 行未改动56575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121136061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014111711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814514614714814915015115215315417 行未改动17217317417517517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723817617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224224324424524624724824925025125225325424324424524624724824925025125225325425525625725825916 行未改动27627727827927928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133228028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633633733833934034134234334434534634734833733833934034134234334434534634734834935035155 行未改动int n, res[N];string s[N], t;
struct AC { int idx; int tr[N][26]; int f[N]; int st[N]; void clear() { for (int i = 0; i <= idx; ++ i ) for (int j = 0; j < 26; ++ j ) tr[i][j] = 0; idx = 0; memset(f, 0, sizeof f); memset(st, 0, sizeof st); } void modify(string s, int id) { int p = 0; for (char c : s) { int w = c - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } st[p] = id; } void build() { queue<int> q; for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { q.push(tr[0][i]); f[tr[0][i]] = 0; } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; q.push(tr[u][i]); } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char c : t) { int w = c - 'a'; p = tr[p][w]; for (int q = p; q; q = f[q]) res[st[q]] ++ ; } } int idx; int tr[N][26]; int f[N]; int st[N]; void clear() { for (int i = 0; i <= idx; ++ i ) for (int j = 0; j < 26; ++ j ) tr[i][j] = 0; idx = 0; memset(f, 0, sizeof f); memset(st, 0, sizeof st); } void modify(string s, int id) { int p = 0; for (char c : s) { int w = c - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } st[p] = id; } void build() { queue<int> q; for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { q.push(tr[0][i]); f[tr[0][i]] = 0; } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; q.push(tr[u][i]); } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char c : t) { int w = c - 'a'; p = tr[p][w]; for (int q = p; q; q = f[q]) res[st[q]] ++ ; } }}T;
void solve() { T.clear(); for (int _ = 1; _ <= n; ++ _ ) { cin >> s[_]; T.modify(s[_], _); } memset(res, 0, sizeof res); cin >> t; T.build(); T.query(t); vector<int> ans; int mx = -1; for (int i = 1; i <= n; ++ i ) { if (res[i] > mx) { mx = res[i]; ans = {i}; } else if (res[i] == mx) { ans.push_back(i); } } cout << mx << '\n'; for (int v : ans) cout << s[v] << '\n'; T.clear(); for (int _ = 1; _ <= n; ++ _ ) { cin >> s[_]; T.modify(s[_], _); } memset(res, 0, sizeof res); cin >> t; T.build(); T.query(t); vector<int> ans; int mx = -1; for (int i = 1; i <= n; ++ i ) { if (res[i] > mx) { mx = res[i]; ans = {i}; } else if (res[i] == mx) { ans.push_back(i); } } cout << mx << '\n'; for (int v : ans) cout << s[v] << '\n';}
signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); while (cin >> n, n) solve(); return 0; ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); while (cin >> n, n) solve(); return 0;}
```
---
17 行未改动int n, res[N];string s[N], t;
struct AC { int tr[N][26], idx, cnt[N]; vector<int> id[N]; // 哪些模式串以这个点结尾 int f[N]; vector<int> g[N]; int d[N], tag[N]; void modify(string s, int i) { int p = 0; for (char _ : s) { int w = _ - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } id[p].push_back(i); } void build() { queue<int> q; for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { f[tr[0][i]] = 0; q.push(tr[0][i]); } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; d[f[tr[u][i]]] ++ ; q.push(tr[u][i]); } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char _ : t) { int w = _ - 'a'; p = tr[p][w]; tag[p] ++ ; } } void topo() { queue<int> q; for (int i = 1; i <= idx; ++ i ) if (!d[i]) q.push(i); while (q.size()) { int u = q.front(); q.pop(); int v = f[u]; tag[v] += tag[u]; if (! -- d[v]) q.push(v); } for (int i = 1; i <= idx; ++ i ) for (int j : id[i]) res[j] += tag[i]; } int tr[N][26], idx, cnt[N]; vector<int> id[N]; // 哪些模式串以这个点结尾 int f[N]; vector<int> g[N]; int d[N], tag[N]; void modify(string s, int i) { int p = 0; for (char _ : s) { int w = _ - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } id[p].push_back(i); } void build() { queue<int> q; for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { f[tr[0][i]] = 0; q.push(tr[0][i]); } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; d[f[tr[u][i]]] ++ ; q.push(tr[u][i]); } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char _ : t) { int w = _ - 'a'; p = tr[p][w]; tag[p] ++ ; } } void topo() { queue<int> q; for (int i = 1; i <= idx; ++ i ) if (!d[i]) q.push(i); while (q.size()) { int u = q.front(); q.pop(); int v = f[u]; tag[v] += tag[u]; if (! -- d[v]) q.push(v); } for (int i = 1; i <= idx; ++ i ) for (int j : id[i]) res[j] += tag[i]; }}T;
signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++ i ) { cin >> s[i]; T.modify(s[i], i); } T.build(); cin >> t; T.query(t); T.topo(); for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n'; return 0; ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++ i ) { cin >> s[i]; T.modify(s[i], i); } T.build(); cin >> t; T.query(t); T.topo(); for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n'; return 0;}```
### 优化16 行未改动int n, res[N];string s[N], t;
struct AC { int tr[N][26], idx; int f[N]; int d[N], tag[N]; int q[N], l = 1, r; vector<int> id[N]; void modify(string s, int i) { int p = 0; for (char _ : s) { int w = _ - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } id[p].push_back(i); } void build() { for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { q[ ++ r] = tr[0][i]; } while (l <= r) { int u = q[l ++ ]; for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; q[ ++ r] = tr[u][i]; } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char _ : t) { int w = _ - 'a'; p = tr[p][w]; tag[p] ++ ; } } void work() { for (int i = r; i; -- i ) { int x = q[i], y = f[x]; tag[y] += tag[x]; } for (int i = 1; i <= idx; ++ i ) for (int j : id[i]) res[j] += tag[i]; } int tr[N][26], idx; int f[N]; int d[N], tag[N]; int q[N], l = 1, r; vector<int> id[N]; void modify(string s, int i) { int p = 0; for (char _ : s) { int w = _ - 'a'; if (!tr[p][w]) tr[p][w] = ++ idx; p = tr[p][w]; } id[p].push_back(i); } void build() { for (int i = 0; i < 26; ++ i ) if (tr[0][i]) { q[ ++ r] = tr[0][i]; } while (l <= r) { int u = q[l ++ ]; for (int i = 0; i < 26; ++ i ) { if (tr[u][i]) { f[tr[u][i]] = tr[f[u]][i]; q[ ++ r] = tr[u][i]; } else { tr[u][i] = tr[f[u]][i]; } } } } void query(string t) { int p = 0; for (char _ : t) { int w = _ - 'a'; p = tr[p][w]; tag[p] ++ ; } } void work() { for (int i = r; i; -- i ) { int x = q[i], y = f[x]; tag[y] += tag[x]; } for (int i = 1; i <= idx; ++ i ) for (int j : id[i]) res[j] += tag[i]; }} T;
signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++ i ) { cin >> s[i]; T.modify(s[i], i); } T.build(); cin >> t; T.query(t); T.work(); for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n'; return 0; ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++ i ) { cin >> s[i]; T.modify(s[i], i); } T.build(); cin >> t; T.query(t); T.work(); for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n'; return 0;}```