HDU3065(初始化更新很省时间)
#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=5e4+100;
int tree[maxn][30];
int num[maxn];
int fail[maxn];int ans[1005];
int tot = 0,n;
string s[1005];
void init(){
mem(ans,0);tot=0;
mem(tree[0],0);
fail[0]=num[0]=0;
}
void insert_(string s,int t){
int root = 0;
for(int i=0;i<s.length();i++){
int id=s[i]-'A';
if(!tree[root][id]){
tree[root][id]=++tot;
mem(tree[tree[root][id]],0);
fail[tree[root][id]]=0;
num[tree[root][id]]=0;
}
root=tree[root][id];
}
num[root]=t;
}
void getFail(){
queue <int>q;
for(int i=0;i<26;i++){
if(tree[0][i]){
fail[tree[0][i]] = 0;
q.push(tree[0][i]);
}
}
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=0;i<26;i++){
if(tree[now][i]){
fail[tree[now][i]] = tree[fail[now]][i];
q.push(tree[now][i]);
}
else
tree[now][i] = tree[fail[now]][i];
}
}
}
void query(string S){
int now = 0,maxx=0;
for(int i=0;i<S.length();i++){
if(S[i]<'A'||S[i]>'Z') {now=0;continue;}
now = tree[now][S[i]-'A'];
for(int j=now;j;j=fail[j]){
ans[num[j]]++;
}
}
rep(i,1,n){
if(ans[i]){
cout<<s[i]<<": "<<ans[i]<<endl;
}
}
}
int main(){
IO;
while(cin>>n){
init();
rep(i,1,n){
cin>>s[i];
insert_(s[i],i);
}
fail[0]=0;
getFail();
string a;
cin>>a;
query(a);
}
}
面向对象封装版本:
#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
string s[1005];int n;
class ac_auto{
public:
const static int maxn=5e4+100;
int tot,root;
int tree[maxn][30];
int End[maxn];
int fail[maxn];int ans[1005];
int newnode(){
for(int i=0;i<26;i++){
tree[tot][i]=0;
}
End[tot]=0;
return tot++;
}
void init(){
mem(ans,0);tot=0;
root=newnode();
}
void insert_(string s,int t){
int now = root;
for(int i=0;i<s.length();i++){
int id=s[i]-'A';
if(!tree[now][id]){
tree[now][id]=newnode();
}
now=tree[now][id];
}
End[now]=t;
}
void getFail(){
queue <int>q;
for(int i=0;i<26;i++){
if(tree[0][i]){
fail[tree[0][i]] = 0;
q.push(tree[0][i]);
}
}
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=0;i<26;i++){
if(tree[now][i]){
fail[tree[now][i]] = tree[fail[now]][i];
q.push(tree[now][i]);
}
else
tree[now][i] = tree[fail[now]][i];
}
}
}
void query(string S){
int now = root;
for(int i=0;i<S.length();i++){
if(S[i]<'A'||S[i]>'Z') {now=0;continue;}
now = tree[now][S[i]-'A'];
for(int j=now;j;j=fail[j]){
ans[End[j]]++;
}
}
for(int i=1;i<=n;i++){
if(ans[i]){
cout<<s[i]<<": "<<ans[i]<<endl;
}
}
}
}acam;
int main(){
IO;
while(cin>>n){
acam.init();
rep(i,1,n){
cin>>s[i];
acam.insert_(s[i],i);
}
acam.getFail();
string a;
cin>>a;
acam.query(a);
}
}
模板洛谷P3808:
#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn = 2*1e6+9;
int tree[maxn][26];
int cntword[maxn];
int fail[maxn];
int tot = 0;
void insert_(string s){
int root = 0;
for(int i=0;i<s.size();i++){
int id=s[i]-'a';
if(!tree[root][id])
tree[root][id]=++tot;
root=tree[root][id];
}
cntword[root]++;
}
void getFail(){
queue <int>q;
for(int i=0;i<26;i++){
if(tree[0][i]){
fail[tree[0][i]] = 0;
q.push(tree[0][i]);
}
}
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=0;i<26;i++){
if(tree[now][i]){
fail[tree[now][i]] = tree[fail[now]][i];
q.push(tree[now][i]);
}
else
tree[now][i] = tree[fail[now]][i];
}
}
}
int query(string s){
int now = 0,ans = 0;
for(int i=0;i<s.size();i++){
now = tree[now][s[i]-'a'];
for(int j=now;j && cntword[j]!=-1;j=fail[j]){
ans += cntword[j];
cntword[j] = -1;
}
}
return ans;
}
int main() {
int n;
string s;
cin >> n;
for(int i=0;i<n;i++){
cin >> s ;
insert_(s);
}
fail[0] = 0;
getFail();
cin >> s ;
cout << query(s) << endl;
return 0;
}
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define eps 0.000000001
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=2e5+5;
int tree[maxn][26];
int match[maxn];
int fail[maxn];
int flag[maxn];
int tot = 0;
void insert_(string s,int t){
int root=0;
for(int i=0;i<s.length();i++){
int id=s[i]-'a';
if(!tree[root][id])
tree[root][id]=++tot;
root=tree[root][id];
}
if(!flag[root]) flag[root]=t;
match[t]=flag[root];
}
int indeg[maxn],ans[maxn];
void getFail(){
queue <int>q;
for(int i=0;i<26;i++){
if(tree[0][i]){
fail[tree[0][i]] = 0;
q.push(tree[0][i]);
}
}
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=0;i<26;i++){
if(tree[now][i]){
fail[tree[now][i]] = tree[fail[now]][i];
indeg[fail[tree[now][i]]]++;
q.push(tree[now][i]);
}
else
tree[now][i] = tree[fail[now]][i];
}
}
}
string ss[maxn];
int n,vis[maxn];
void query(string s){
int now = 0;
for(int i=0;i<s.length();i++){
now = tree[now][s[i]-'a'];
ans[now]++;
}
}
void topusort(){
queue<int> q;
rep(i,1,tot){
if(!indeg[i]) q.push(i);
}
while(!q.empty()){
int u=q.front();q.pop();
vis[flag[u]]=ans[u];
int v=fail[u];
ans[v]+=ans[u];
if(--indeg[v]==0) q.push(v);
}
}
int main(){
cin>>n;
rep(i,1,n){
cin>>ss[i];
insert_(ss[i],i);
}
getFail();
string s;cin>>s;
query(s);
topusort();
rep(i,1,n){
cout<<vis[match[i]]<<endl;
}
}