本文主要是介绍AC自动机 白书模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模板:
http://acm.hdu.edu.cn/showproblem.php?pid=2222
struct ACauto
{
int ch[maxn][26];
int sz;
int f[maxn],last[maxn],val[maxn],cnt[maxn];
void init()
{
sz=1;
memset(ch[0],0,sizeof ch[0]);
memset(cnt,0,sizeof cnt);
}
int idx(char c)
{
return c-'A';
}
void add(char *s,int v)
{
int u=0,len=strlen(s);
for(int i=0;i<len;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
void getfail()
{
queue<int>q;
f[0]=0;
for(int c=0;c<26;c++)
{
int u=ch[0][c];
if(u)
{
f[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<26;c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[f[r]][c];
continue;
}
q.push(u);
f[u]=ch[f[r]][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void print(int j)
{
if(j)
{
cnt[val[j]]++;
print(last[j]);
}
}
void Find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else if(last[j])print(last[j]);
}
}
}ac;
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38991 Accepted Submission(s): 12571Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.InputFirst line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.OutputPrint how many keywords are contained in the description.Sample Input1 5 she he say shr her yasherhsSample Output3AuthorWiskeyRecommendlcy
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<string>
#include<queue>
#define maxn 250010
using namespace std;
struct ACauto
{
int ch[maxn][26];
int sz;
int f[maxn],last[maxn],val[maxn][2];
int num;
int used[maxn];
void init()
{
num=0;
sz=1;
memset(ch[0],0,sizeof(ch[0]));
memset(used,0,sizeof used);
memset(val,0,sizeof(val));
}
int idx(char c)
{
return c-'a';
}
void add(char *s,int v)
{
int u=0,len=strlen(s);
for(int i=0;i<len;i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz][0]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u][0]=v;//???????á??±ê??
val[u][1]++;
}
void print(int j)
{
if(j&&!used[val[j][0]])
{
used[val[j][0]]=1;
num+=val[j][1];
print(last[j]);
}
}
void getfail()
{
queue<int>q;
f[0]=0;
for(int c=0;c<26;c++)
{
int u=ch[0][c];
if(u)
{
f[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<26;c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[f[r]][c];
continue;
}
q.push(u);
f[u]=ch[f[r]][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void Find(char*T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c=idx(T[i]);
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else if(last[j])print(last[j]);
}
}
}ac;
char s[60],T[1000010];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
ac.init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
ac.add(s,i);
}
ac.getfail();
scanf("%s",T);
ac.Find(T);
printf("%d\n",ac.num);
}
return 0;
}
这篇关于AC自动机 白书模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!