In 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.
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.
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.
1 5 she he say shr her yasherhs
3
AC自动机这方面的题我感觉比较难理解,因此呢写写题解让自己更加深刻理解吧,如果也能帮助到别人就更好了。
首先就这个题来说:
---------------------------------------------题意------------------------------------------------------------------
给你多个模板组成的字典和一个文本,问你文本中出现了多少个模板。
分析的话: 首先建立AC自动机,然后标记好模板,再用文本串去匹配AC自动机就好,其他的在代码中会体现。
基本上就是对于模板的改动。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxnode=500100;
const int sigma_size=26;
int ans;
struct AC_Automata
{
int ch[maxnode][sigma_size];
int val[maxnode]; //用来保存当前单词输入的次数
int f[maxnode]; // fail函数
int last[maxnode]; // last[i]=j表j节点表示的单词是i节点单词的后缀,且j节点是单词节点
int sz;
bool vis[maxnode];//这个是根据题意添加的用来表示单词结点 非单词结点vis=0,vis=1.
//初始化0号根节点的相关信息
void init()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
vis[0]=false;
last[0]=f[0]=val[0]=0;
}
//insert负责构造ch与val数组
//插入字符串,v必须非0表示一个单词节点
void insert(char *s)
{
int n=strlen(s),u=0;
for(int i=0; i<n; i++)
{
int id=s[i]-'a';
if(ch[u][id]==0)
{
vis[sz]=false;
ch[u][id]=sz;
memset(ch[sz],0,sizeof(ch[sz]));
val[sz++]=0;
}
u=ch[u][id];
}
vis[u]=true;
val[u]++;
}
//getFail函数负责构造f和last数组
void getFail()
{
queue<int> q;
f[0]=0;
for(int i=0; i<sigma_size; i++)
{
int u=ch[0][i];
if(u)
{
f[u]=last[u]=0;
q.push(u);
}
}
while(!q.empty())// 按BFS顺序计算fail
{
int r=q.front(); q.pop();
for(int i=0; i<sigma_size; i++)
{
int u=ch[r][i];
if(u==0)continue;
q.push(u);
int v=f[r];
while(v && ch[v][i]==0) v=f[v];
f[u]= ch[v][i];
last[u] = val[f[u]]?f[u]:last[f[u]];
}
}
}
//递归打印与结点i后缀相同的前缀节点编号
//进入此函数前需保证val[i]>0
void print(int i)
{
if(val[i])
{
if(vis[i])
{
ans+=val[i];
vis[i]=false;//找到单词的话,令vis=false.
}
print(last[i]);
}
}
// 在s中找出出现了哪几个模板单词
void find(char *s)
{
int n=strlen(s),j=0;
for(int i=0;i<n;i++)
{
int id=s[i]-'a';
while(j && ch[j][id]==0) j=f[j];
j=ch[j][id];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
};
AC_Automata ac;
const int MAXN=1000000+100;
char text[MAXN],word[100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ans=0;
ac.init();
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",word);
ac.insert(word);
}
ac.getFail();
scanf("%s",text);
ac.find(text);
printf("%d\n",ans);
}
return 0;
}