标签:queue i++ ring down strlen iostream ios source while
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, len, u, t, siz;
char a[1000005];
queue<int> d;
struct aczdj{
int s[1000005][26], fai[1000005], hav[1000005];
void ins(){
len = strlen(a);
u = 0;
for(int i=0; i<len; i++){
t = a[i] - ‘a‘;
if(!s[u][t]) s[u][t] = ++siz;
u = s[u][t];
}
hav[u]++;
}
void getFail(){
for(int i=0; i<26; i++)
if(s[0][i])
fai[s[0][i]] = 0, d.push(s[0][i]);
while(!d.empty()){
u = d.front();
d.pop();
for(int i=0; i<26; i++){
if(s[u][i]){
fai[s[u][i]] = s[fai[u]][i];
d.push(s[u][i]);
}
else s[u][i] = s[fai[u]][i];
}
}
}
int query(){
int ans=0;
len = strlen(a);
u = 0;
for(int i=0; i<len; i++){
u = s[u][a[i]-‘a‘];
for(int j=u; j && hav[j]!=-1; j=fai[j]){
ans += hav[j];
hav[j] = -1;
}
}
return ans;
}
}ac;
int main(){
cin>>n;
while(n--) scanf("%s", a), ac.ins();
ac.getFail();
scanf("%s", a);
printf("%d\n", ac.query());
return 0;
}
标签:queue i++ ring down strlen iostream ios source while
原文地址:http://www.cnblogs.com/poorpool/p/7989555.html