题意:n个模式串,一个待匹配串,求求每个模式串在待匹配串中出现次数。
思路:记录模式串尾端,然后遍历统计。
#include <bits/stdc++.h> using namespace std; const int N = 50*1010; int n; char a[1010][60]; namespace AC { int tr[N][130], tot; int fail[N],e[N]; int v[N]; int val[1010]; void insert(char *s,int w) { int u = 0; for (int i = 1; s[i]; i++) { if (!tr[u][s[i]]) tr[u][s[i]] = ++tot; u = tr[u][s[i]]; } e[u]++; v[u]=w; } queue<int> q; void build() { for (int i = 0; i < 130; i++) if (tr[0][i]) q.push(tr[0][i]); while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 130; i++) { if (tr[u][i]) fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]); else tr[u][i] = tr[fail[u]][i]; } } } int query(char *t) { int u = 0, res = 0; int k=0; memset(val,0,sizeof(val)); for (int i = 1; t[i]; i++) { u = tr[u][t[i]]; // 转移 for (int j = u; j && e[j] != 0; j = fail[j]) { if(v[j]) { val[v[j]]++; } } } for(int i=1;i<=n;i++) { if(val[i]) { printf("%s: %d ",a[i]+1,val[i]); } } } void init(){ memset(tr,0,sizeof(tr)); tot=0; memset(fail,0,sizeof(fail)); memset(e,0,sizeof(e)); } } // namespace AC char s[2000010]; int main() { int ans=0; while(scanf("%d", &n)!=EOF){ AC::init(); for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1), AC::insert(a[i],i); int t; AC::build(); scanf("%s", s + 1); AC::query(s); } return 0; }