【AC自动机】Hdu2222:Keywords Search
2016-02-16 19:29
405 查看
Hdu 2222:Keywords Search
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
15
she
he
say
shr
her
yasherhs
Sample Output
3Solution
裸的ac自动机注意读懂题意:只要出现了ans+1,不管几次
坑点:字典会出现多个单词
第一次自己手打模版
好慢TAT 有待改进
然而感觉很爽
Code
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define maxn 10001 char s[10001][51],T[maxn*100]; int len[maxn*100],P[maxn*100],trie[maxn*100][26],cnt,poi,an,fail[maxn*100],temp,dog,ok[maxn*100]; void work(int dig) { while(!trie[poi][dig]&&poi)poi=fail[poi]; poi=trie[poi][dig]; if(!poi)return; temp=poi; while(temp) { temp=fail[temp]; if(ok[temp])an+=ok[temp],ok[temp]=0; } if(ok[poi]) { an+=ok[poi],ok[poi]=0,temp=poi; } } void add(int Poi,int dig) { if(!trie[Poi][dig]) { trie[Poi][dig]=++cnt;dog=cnt; while(1) { if(trie[fail[Poi]][dig]){fail[cnt]=trie[fail[Poi]][dig];break;} else if(!trie[fail[Poi]][dig]&&Poi==0)break; else Poi=fail[Poi]; } } else dog=trie[Poi][dig]; } int main() { freopen("2222.in","r",stdin); freopen("2222.ans","w",stdout); int TT,L; scanf("%d",&TT); while(TT--) { cnt=0;an=0; int n,mx=0,p=0; memset(s,0,sizeof(s)); memset(trie,0,sizeof(trie)); memset(ok,0,sizeof(ok)); memset(T,0,sizeof(T)); memset(P,0,sizeof(P)); memset(fail,0,sizeof(fail)); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%s",s[i]),len[i]=strlen(s[i]),mx=max(mx,len[i]); for(int i=1;i<=n;i++) { if(!trie[0][s[i][0]-'a'])trie[0][s[i][0]-'a']=++cnt; P[i]=trie[0][s[i][0]-'a']; if(len[i]==1)ok[P[i]]++; } for(int i=1;i<mx;i++) for(int j=1;j<=n;j++) { if(i<len[j])add(P[j],s[j][i]-'a'),P[j]=dog; if(i==len[j]-1)ok[P[j]]++; } scanf("%s",T); L=strlen(T); for(int i=0;i<L;i++) work(T[i]-'a'); printf("%d\n",an); } }
相关文章推荐
- 使用Qt设计师实现对话框
- 驱动开发误用指针错误:Unable to handle kernel NULL pointer dereference at virtual address
- NOIP2015普及组总结
- Python笔记(1)——简介和安装
- 【android笔记】之 android studio (一)
- 209 Minimum Size Subarray Sum
- hive udaf 用maven打包运行create temporary function 时报错
- [总结]数学专题(持续更新)
- 【BZOJ 3036】 绿豆蛙的归宿
- 新的一年,我们如何才能收获满满,不留太多遗憾呢?
- Activity生命周期详解
- 试用阿里云RDS
- 初识Universal-Image-Loader
- 1分钟学会Markdown语法
- 位置参数变量
- 109. Convert Sorted List to Binary Search Tree LeetCode
- 在线PDF编辑网站http://www.pdfescape.com
- 河南第五届ACM省赛(Divideing Jewels)
- 开发中遇见的问题
- 在线PDF编辑网站http://www.pdfescape.com