隐藏页面特效

HDU - 2222 Keywords Search

 
Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u

 Status

Description

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. 
 

Input

First 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. 
 

Output

Print how many keywords are contained in the description.
 

Sample Input

1 5 she he say shr her yasherhs
 

Sample Output

3
 

AC自动机的模板
2017-04-07
#include<cstdio> #include<cstring> #define newnode ++cnt #define m(s) memset(s,0,sizeof s); using namespace std; const int N=5e5+5; const int Sz=26; int n,T,root,cnt,ans; int q[N],fail[N]; int tr[N][Sz],tag[N]; char s[N<<1];bool mark[N]; void insert(){ int now=root,len=strlen(s); for(int i=0,t;i<len;i++){ t=s[i]-'a'; if(!tr[now][t]) tr[now][t]=newnode; now=tr[now][t]; } tag[now]++; } void acmach(){ for(int i=0;i<Sz;i++) tr[0][i]=root; int h=0,t=1;q[t]=root;fail[root]=0; while(h!=t){ int now=q[++h]; for(int i=0,p;i<Sz;i++){ if(!tr[now][i]) continue; for(p=fail[now];!tr[p][i];p=fail[p]); p=tr[p][i]; fail[tr[now][i]]=p; q[++t]=tr[now][i]; } } } void solve(){ int p=root,len=strlen(s); for(int i=0,t;i<len;i++){ t=s[i]-'a'; mark[p]=1; for(;!tr[p][t];p=fail[p]); p=tr[p][t]; if(!mark[p]){ for(int j=p;j;j=fail[j]){ ans+=tag[j]; tag[j]=0; } } } printf("%d\n",ans); } void Clear(){ ans=0;cnt=0; m(tr);m(tag);m(fail);m(mark); } int main(){ for(scanf("%d",&T);Clear(),T--;){ scanf("%d",&n);root=newnode; for(int i=1;i<=n;i++) scanf("%s",s),insert(); acmach(); scanf("%s",s); solve(); } return 0; }

 

/* tr[j][t]=i 表示j节点的第t个儿子是编号是i j=fail[i] 表示在i节点之前输入的节点中找到与i的字母相同的节点j, 并且j之前的字符一定与i之前的字符相吻合, 注意刚开始所有的fail[i]=0,到最后只有fail[1]=0; danger[i]表示以i节点结尾的的单词个数;
值得注意的是,根节点的编号不能是0。因为kmp的fail[]会退到0
*/ #include<cstdio> #include<cstring> #include<algorithm> #define m(s) memset(s,0,sizeof s) using namespace std; const int N=5e5+10; int T,n,ans,cnt,q[N],fail[N],danger[N],tr[N][26]; char s1[51],s2[N<<1];bool mark[N]; void Cl(){ cnt=1;ans=0; m(fail);m(mark);m(danger);m(tr); for(int i=0;i<26;i++) tr[0][i]=1; } void ins(){//建树 int now=1,len=strlen(s1);//now=1是root for(int i=0;i<len;i++){ int t=s1[i]-'a'; if(!tr[now][t]) tr[now][t]=++cnt;//若没有编号,则生成编号为++cnt now=tr[now][t]; //如果当前节点有编号,将父亲的节点编号赋值为当前节点编号,以便于下个节点的使用 } danger[now]++;/*到每个单词的最后一个结点的时候,最后的now值便同最后一个节点的编号相等 因为最后一个节点此时已经没有儿子*/ } void acmach(){//在tire上搞kmp int h=0,t=1,now; q[1]=1;fail[1]=0;//root入队 while(h!=t){ now=q[++h]; for(int i=0;i<26;i++){ if(!tr[now][i]) continue;//找以now为父亲的节点 int p=fail[now];//找now的字母在先前出现过的节点为p while(!tr[p][i]) p=fail[p];//直到p的儿子与now的儿子字母相同 fail[tr[now][i]]=tr[p][i];//将now的儿子的与p的儿子编号匹配 q[++t]=tr[now][i];//将now的儿子入队 } } } void solve(){//查找s2字符串的模式串 int p=1,len=strlen(s2); for(int i=0;i<len;i++){ int t=s2[i]-'a'; mark[p]=1; while(!tr[p][t]) p=fail[p]; p=tr[p][t];//找到第一个可以进行匹配的节点 if(!mark[p]){ for(int j=p;j;j=fail[j]){ /*不断利用失败数组找可以进行匹配的节点,直到fail[j]=0*/ ans+=danger[j];//答案加上以该字母作为结束点的单词数 danger[j]=0; } } } printf("%d\n",ans); } int main(){ for(scanf("%d",&T);Cl(),T--;){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%s",s1),ins(); acmach(); scanf("%s",s2); solve(); } return 0; }

 

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/5470163.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(402)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示