#include<set> #include<map> #include<stack> #include<queue> #include<ctime> #include<cmath> #include<cstdio> #include<vector> #include<bitset> #include<string> #include<cstring> #include<sstream> #include<cstdlib> #include<iostream> #include<algorithm> #include<functional> #define eps 1e-8 #define PI acos(-1.0) #define ll long long usingnamespace std; constint mod = 1e9 + 7; constint INF = 0x3f3f3f3f; constint maxn = 1005; structnode{ char s[51]; int cnt; int pos; }a[maxn]; constint kind = 130; int num[1005], vis[100005],mark[100005],nxt[100005][130], fail[100005]; int cnt; intnewnode() { ++cnt; for(int i = 0; i < kind; ++i) { nxt[cnt][i] = -1; } vis[cnt] = 0; mark[cnt] = 0; return cnt; }
voidinit(){ cnt = -1; newnode(); }
voidword_insert(char s[], int pos) { int sz = strlen(s); int now = 0; for(int i = 0; i < sz; ++i) { int tmp = s[i]; if(nxt[now][tmp] == -1) nxt[now][tmp] = newnode(); now = nxt[now][tmp]; } mark[now] = pos; }
voidbuild() { fail[0] = 0; queue<int>Q; for(int i = 0 ; i < kind; ++i) { if(nxt[0][i] == -1) nxt[0][i] = 0; else{ fail[nxt[0][i]] = 0; Q.push(nxt[0][i]); } } while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i = 0; i < kind; ++i) { if(nxt[now][i] == -1) nxt[now][i] = nxt[fail[now]][i]; else{ fail[nxt[now][i]] = nxt[fail[now]][i]; Q.push(nxt[now][i]); } } } }
char s[2000005];
voidac_auto(char s[]) { int sz = strlen(s); int now = 0; for(int i = 0; i < sz; ++i) { if(s[i] < 'A' || s[i] > 'Z') {now = 0; continue;} now = nxt[now][s[i]]; int tmp = now; while(tmp != 0) { num[mark[tmp]]++; tmp = fail[tmp]; } } }