HDU2222(AC自动机)
来源:互联网 发布:中信证券交易软件下载 编辑:程序博客网 时间:2024/06/08 17:53
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.
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
题目的大体意思就是:有T组数据,给出n个单词作为字典,一个s字符串,查找s中出现多少个字典中的单词
分析:AC自动机必做题,这个自动机是自己搞的,O(∩_∩)O~呵呵
主要步骤分三步:
1.构建trie树
2.构建失配指针
3.像自动机一样奔跑
这里写代码片#include<cstdio>#include<cstring>#include<iostream>#include<queue>using namespace std;int ch[50*10010][30];char w[10010][51];char s[1000010];int n,T,tot,fail[50*10010];int iend[50*10010];bool p[50*10010];void build(int bh){ int i,j,now=0; int len=strlen(w[bh]); for (i=0;i<len;i++) { int x=w[bh][i]-'a'; if (!ch[now][x]) ch[now][x]=++tot; now=ch[now][x]; } iend[now]++; return;}void make(){ int i,j; queue<int> q; for (i=0;i<26;i++) if (ch[0][i]) q.push(ch[0][i]); while (!q.empty()) { int r=q.front(); q.pop(); for (i=0;i<26;i++) { if (!ch[r][i]) { ch[r][i]=ch[fail[r]][i]; continue; } fail[ch[r][i]]=ch[fail[r]][i]; q.push(ch[r][i]); } } return;}void solve() //0是根节点 { memset(p,1,sizeof(p)); int len=strlen(s); int ans=0; int now=0,i; for (i=0;i<len;i++) { int x=s[i]-'a'; while (!ch[now][x]&&now!=0) now=fail[now]; //他没有这个儿子,就找失配 //找到根节点还没找到就停止了 now=ch[now][x]; //儿子 int f=now; while (f) //回溯,直到根 { if (!p[f]) break; else if (p[f]&&iend[f]>0) //从未访问过 { ans+=iend[f]; p[f]=0; } f=fail[f]; //往失配上找,看看失配上有没有可以匹配的单词 } } printf("%d\n",ans); return;}int main(){ scanf("%d",&T); while (T--) { memset(iend,0,sizeof(iend)); //千万不要忘记初始化 memset(ch,0,sizeof(ch)); memset(fail,0,sizeof(fail)); tot=0; memset(w,'\0',sizeof(w)); memset(s,'\0',sizeof(s)); scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%s",&w[i]); build(i); } make(); scanf("%s",&s); solve(); } return 0;}
- hdu2222,(ac自动机)
- hdu2222(AC自动机)
- HDU2222(AC自动机)
- AC自动机(hdu2222)
- hdu2222(AC自动机)
- AC自动机模板(hdu2222)
- ac自动机模板(hdu2222)
- hdu2222(AC自动机基础)
- ac自动机模板(hdu2222)
- hdu2222 Keywords Search (AC自动机)
- AC自动机模板(数组+指针)hdu2222
- hdu2222 Keywords Search (AC自动机)
- [HDU2222]Keywords Search(AC自动机)
- HDU2222 Keywords Search(AC自动机)
- HDU2222 多串匹配 (AC自动机)
- [HDU2222]Keywords Search(AC自动机)
- HDU2222【AC自动机(基础·模板)】
- hdu2222:Keywords Search(AC自动机)
- 类的初始化
- 汇编语言-实现在指定字符串中搜索字符'A'
- MySQL 忘记root密码及MySQL修改root密码的方法
- 姑娘留步,容我劫个色
- Android ANR问题介绍和处理方法
- HDU2222(AC自动机)
- 超级素数幂
- jQuery基础学习DOM对象与jQuery对象
- Wireshark之网络通信原理(三)
- SCU4488 king's trouble II(DP)
- Java跨平台原理
- 3. 效果评估(效果回归)
- Unknown character set: 'utf8mb4'
- linux系统下从/proc中找回误删除的控制文件