HDU2222 -ac自动机
来源:互联网 发布:成都淘宝代运营 编辑:程序博客网 时间:2024/06/01 19:14
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 63350 Accepted Submission(s): 21015
Problem 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.
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.
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
15shehesayshrheryasherhs
Sample Output
3
Author
Wiskey
题目大意:
给n 个字母,以及一个串,问这个串中有多少个字母,不能重复计数
题目思路:
AC自动机模板,对于AC自动机的讲解可以看这篇博文:ac自动机详解
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<iostream>using namespace std;const int maxn = 1e6+10;const int maxtot = 5e5+10;int Size;struct Tree{ int nex[26]; int fail,cnt; void init() { fail = cnt = 0; memset(nex,0,sizeof(nex)); }}node[maxtot];void Insert(char *s){ int n = strlen(s); int now = 0; for(int i=0;i<n;i++) { int id = s[i]-'a'; if(!node[now].nex[id]) { node[now].nex[id] = Size++; } now = node[now].nex[id]; } node[now].cnt++;}queue<int>q;void Build(){ node[0].fail = -1; q.push(0); while(!q.empty()) { int u = q.front(); q.pop(); for(int i=0;i<26;i++) { if(node[u].nex[i]) { if(u==0)node[node[u].nex[i]].fail = 0; else { int v = node[u].fail ; while(v!=-1) { if(node[v].nex[i]) { node[node[u].nex[i]].fail = node[v].nex[i]; break; } v = node[v].fail; } if(v==-1)node[node[u].nex[i]].fail = 0; } q.push(node[u].nex[i]); } } }}int Search(char *s){ int n = strlen(s); int res = 0,now = 0; for(int i=0;i<n;i++) { int id = s[i]-'a'; if(node[now].nex[id])now = node[now].nex[id]; else { int p = node[now].fail; while(p!=-1&&node[p].nex[id]==0)p = node[p].fail; if(p==-1)now = 0; else now = node[p].nex[id]; } if(node[now].cnt) { int uu = now; while(uu) { res+=node[uu].cnt; node[uu].cnt = 0; uu = node[uu].fail; } } } return res;}void init(){ for(int i=0;i<maxtot;i++) node[i].init(); Size = 1;}char s[maxn];int main(){ int t;cin>>t; while(t--) { int n; scanf("%d",&n); init(); for(int i=0;i<n;i++) { scanf("%s",s); Insert(s); } Build(); scanf("%s",s); printf("%d\n",Search(s)); } return 0;}
阅读全文
0 0
- hdu2222 ac自动机模版
- hdu2222 AC自动机
- hdu2222,(ac自动机)
- HDU2222-tire,AC自动机
- AC自动机模板 hdu2222
- hdu2222-AC自动机
- HDU2222 AC自动机
- hdu2222 AC自动机
- 【AC自动机】HDU2222
- hdu2222 AC自动机
- hdu2222 ac自动机。。。。
- HDU2222 AC自动机入门
- hdu2222 ac自动机模板
- hdu2222 AC自动机
- 初识AC自动机 HDU2222
- hdu2222(AC自动机入门)
- AC自动机 hdu2222
- HDU2222 AC自动机模板
- CPU寻址能力,地址总线宽度,Byte,bit,怎么算?
- 文章标题
- 深入理解LSTM
- 系统管理系列——02Windows Server 2012 R2怎么添加Windows Server Backup 功能
- 暑期项目开发实训 Day1
- HDU2222 -ac自动机
- git status 查看状态 与 git diff 查看改动
- Struts2-概述
- 打印9*9乘法表
- mysql 修改密码
- windows下Python安装pymysql
- 两个元素交换,粗心导致的BUG!!!
- spring -boot的jre版本问题Java.lang.UnsupportedClassVersionError: org/apache/nutch/crawl/Crawl3 : Unsuppor
- 11、配置环境(environments)