HDU 2222 Keywords Search AC自动机
来源:互联网 发布:把mac里的文件拷到u盘 编辑:程序博客网 时间:2024/06/15 02:11
A - Keywords Search
Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Submit
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
模板题 直接套
#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<stack>#include<set>#include<bitset>using namespace std;const int MAXN=1000000+5;const int SIZE=26;const int MAXL=50+5;char S[MAXN],P[MAXL];int cnt,node[MAXN][SIZE],isWord[MAXN],Next[MAXN];void Init(){ cnt=1; memset(node[0],0,sizeof(node[0])); memset(isWord,0,sizeof(isWord));}void Insert(char* str){ int len=strlen(str); int now=0; for (int i=0;i<len;i++) { int num=str[i]-'a'; if (!node[now][num]) { memset(node[cnt],0,sizeof(node[cnt])); isWord[cnt]=0; node[now][num]=cnt++; } now=node[now][num]; } isWord[now]++;}void GetNext(){ queue<int> Q; Next[0]=0; for (int num=0;num<SIZE;num++) { int u=node[0][num]; if (u) { Q.push(u); Next[u]=0; } } while (!Q.empty()) { int now=Q.front(); Q.pop(); for (int num=0;num<SIZE;num++) { int u=node[now][num]; if (!u) { node[now][num]=node[Next[now]][num]; continue; } Next[u]=node[Next[now]][num]; Q.push(u); } }}int Solve(char* str){ int len=strlen(str); int u=0,Ans=0; for (int i=0;i<len;i++) { int num=str[i]-'a'; u=node[u][num]; int v=u; while (v&&isWord[v]) { Ans+=isWord[v]; isWord[v]=0; v=Next[v]; } } return Ans;}int main(){ cin.sync_with_stdio(false); int T,N; cin>>T; while (T--) { Init(); cin>>N; for (int i=1;i<=N;i++) { cin>>P; Insert(P); } GetNext(); cin>>S; cout<<Solve(S)<<endl; } return 0;}
- HDU 2222 Keywords Search (AC自动机)
- hdu 2222 Keywords Search //ac自动机
- hdu 2222 Keywords Search AC自动机详解
- hdU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search--AC自动机
- hdu 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search 【AC自动机模板】
- hdu 2222 Keywords Search AC自动机
- HDU 2222 Keywords Search (AC自动机)
- hdu - 2222 - Keywords Search(AC自动机)
- [HDU 2222]Keywords Search[AC自动机]
- hdu 2222 Keywords Search----AC自动机
- hdu 2222 Keywords Search(AC自动机)
- AFNetworking 批量上传统一返回图片的3种方法
- 二叉树
- HDU 2050 折线分平面
- 10048--- AtomicInteger的介绍和使用
- 数据库事务ACID
- HDU 2222 Keywords Search AC自动机
- 使用Jsoup爬网页出现500错误
- 查找算法
- webrtc 音频VAD 检测
- HDU 2896 病毒侵袭 AC自动机
- Java StringBuffer和StringBuilder类
- 记一次前段调试问题
- 解决并发问题
- 上海快递代理点信息-百度坐标