【模板】AC自动机
#include <queue> #include <cstdio> #include <cstring> #define N 1000001 int n, cnt, ans; int next[N][26], val[N], fail[N]; char s[N]; std::queue <int> q; inline void insert() { int i, x, now = 0, m = strlen(s + 1); for(i = 1; i <= m; i++) { x = s[i] - 'a'; if(!next[now][x]) next[now][x] = ++cnt; now = next[now][x]; } val[now]++; } inline void make_fail() { int i, now; for(i = 0; i < 26; i++) if(next[0][i]) q.push(next[0][i]); while(!q.empty()) { now = q.front(); q.pop(); for(i = 0; i < 26; i++) { if(!next[now][i]) { next[now][i] = next[fail[now]][i]; continue; } fail[next[now][i]] = next[fail[now]][i]; q.push(next[now][i]); } } } inline void ac() { int i, j, now = 0, m = strlen(s + 1); for(i = 1; i <= m; i++) { now = next[now][s[i] - 'a']; for(j = now; j && ~val[j]; j = fail[j]) { ans += val[j]; val[j] = -1; } } } int main() { int i; scanf("%d", &n); for(i = 1; i <= n; i++) { scanf("%s", s + 1); insert(); } make_fail(); scanf("%s", s + 1); ac(); printf("%d\n", ans); return 0; }
#include <queue> #include <cstdio> #include <cstring> #define N 1000001 #define max(x, y) ((x) > (y) ? (x) : (y)) int n, cnt; int val[N], next[N][26], ans[N], fail[N]; char s1[151][71], s2[N]; std::queue <int> q; inline void insert(int k, char *s) { int i, x, now = 0, m = strlen(s + 1); for(i = 1; i <= m; i++) { x = s[i] - 'a'; if(!next[now][x]) next[now][x] = ++cnt; now = next[now][x]; } val[now] = k; } inline void make_fail() { int i, now; for(i = 0; i < 26; i++) if(next[0][i]) q.push(next[0][i]); while(!q.empty()) { now = q.front(); q.pop(); for(i = 0; i < 26; i++) { if(!next[now][i]) { next[now][i] = next[fail[now]][i]; continue; } fail[next[now][i]] = next[fail[now]][i]; q.push(next[now][i]); } } } inline void ac(char *s) { int i, j, now = 0, m = strlen(s + 1); for(i = 1; i <= m; i++) { now = next[now][s[i] - 'a']; for(j = now; j; j = fail[j]) if(val[j]) ans[val[j]]++; } for(i = 1; i <= n; i++) ans[0] = max(ans[0], ans[i]); printf("%d\n", ans[0]); for(i = 1; i <= n; i++) if(ans[i] == ans[0]) printf("%s\n", s1[i] + 1); } int main() { int i; while(scanf("%d", &n) && n) { cnt = 0; memset(val, 0, sizeof(val)); memset(ans, 0, sizeof(ans)); memset(next, 0, sizeof(next)); memset(fail, 0, sizeof(fail)); for(i = 1; i <= n; i++) { scanf("%s", s1[i] + 1); insert(i, s1[i]); } make_fail(); scanf("%s", s2 + 1); ac(s2); } return 0; }