题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222
思路:模板题
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define lc (d<<1)
#define rc (d<<1|1)
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
const int N=550000;
int n;
string a;
struct AC_auto
{
struct as{
int val;
int ne[26];
void init()
{
val=0;
REW(ne,0);
}}ch[N];
int f[N],last[N],sz;
void clear(){ch[0].init();sz=1;}
int idx(char a){return a-'a';}
void add(string a)
{
int n=a.size(),p=0;
FOR(i,0,n-1)
{
int c=idx(a[i]);
if(ch[p].ne[c]==0)
{
ch[sz].init();
ch[p].ne[c]=sz++;
}
p=ch[p].ne[c];
}
ch[p].val++;
}
void getfail()
{
queue<int> q;
f[0]=0;
int u=0;
FOR(c,0,25)
{
u=ch[0].ne[c];
if(u){f[u]=0;q.push(u);last[u]=0;}
}
while(q.size())
{
int r=q.front();q.pop();
FOR(c,0,25)
{
int u=ch[r].ne[c];
if(!u){ch[r].ne[c]=ch[f[r]].ne[c];continue;}
q.push(u);
int v=f[r];
while(v&&!ch[v].ne[c]) v=f[v];
f[u]=ch[v].ne[c];
last[u]=ch[f[u]].val?f[u]:last[f[u]];
}
}
}
int fid(string a)
{
int u=0,cnt=0,n=a.size();
FOR(i,0,n-1)
{
int c=idx(a[i]);
u=ch[u].ne[c];
int temp=0;
if(ch[u].val) temp=u;
else if(last[u]) temp=last[u];
while(temp)
{
cnt+=ch[temp].val;
ch[temp].val=0;
temp=last[temp];
}
}
return cnt;
}
}tr;
int main()
{
cin.tie(0);
cout.tie(0);
int t;
si(t);
while(t--)
{
si(n);
tr.clear();
while(n--)
{
cin>>a;
tr.add(a);
}
tr.getfail();
cin>>a;
int s=tr.fid(a);
cout<<s<<endl;
}
return 0;
}
还有hdu2896也是道模板题,不过注意不只是小写字母只有26位,大概开到130左右
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define lc (d<<1)
#define rc (d<<1|1)
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
const int N=100008;
int n,m,s[508],zz=1,szz=1;
char a[208],b[10008];
struct AC_auto
{
struct as{
int val;
int ne[132];
void init()
{
val=0;
REW(ne,0);
}}ch[N];
int f[N],last[N],sz;
void clear(){ch[0].init();sz=1;}
int idx(char a){int zz=a;return zz;}
void add(char *a,int x)
{
int n=strlen(a),p=0;
FOR(i,0,n-1)
{
int c=idx(a[i]);
if(ch[p].ne[c]==0)
{
ch[sz].init();
ch[p].ne[c]=sz++;
}
p=ch[p].ne[c];
}
ch[p].val=x;
}
void getfail()
{
queue<int> q;
f[0]=0;
int u=0;
FOR(c,0,130)
{
u=ch[0].ne[c];
if(u){f[u]=0;q.push(u);last[u]=0;}
}
while(q.size())
{
int r=q.front();q.pop();
FOR(c,0,130)
{
int u=ch[r].ne[c];
if(!u){ch[r].ne[c]=ch[f[r]].ne[c];continue;}
q.push(u);
int v=f[r];
while(v&&!ch[v].ne[c]) v=f[v];
f[u]=ch[v].ne[c];
last[u]=ch[f[u]].val?f[u]:last[f[u]];
}
}
}
void fid(char *a)
{
int u=0,n=strlen(a),sz=4,pp=7;
FOR(i,0,n-1)
{
int c=idx(a[i]);
u=ch[u].ne[c];
int temp=0;
if(ch[u].val) temp=u;
else if(last[u]) temp=last[u];
while(temp)
{
s[zz++]=ch[temp].val;
temp=last[temp];
if(zz>=4) break;
}
}
}
}tr;
int main()
{
cin.tie(0);
cout.tie(0);
si(n);
tr.clear();
FOR(i,1,n)
{
ss(a);
tr.add(a,i);
}
tr.getfail();
si(m);
FOR(i,1,m)
{
ss(b);
zz=1;
tr.fid(b);
if(zz==1) continue;
sort(s+1,s+zz);
cout<<"web "<<i<<": ";
FOR(j,1,zz-2) cout<<s[j]<<" ";
cout<<s[zz-1]<<endl;
szz++;
}
cout<<"total: ";
cout<<szz-1<<endl;
return 0;
}