hdu2222之AC自动机入门
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25799 Accepted Submission(s): 8421
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.
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.
5
she
he
say
shr
her
yasherhs
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 9999999
using namespace std; const int MAX=1000000+10;
char a[60],b[MAX]; struct TrieNode{
int num;//记录单词的数量
TrieNode *next[26],*fail;//下一个节点和失败指针
TrieNode(){
num=0;
fail=0;
memset(next,0,sizeof next);
}
}*root; void InsertNode(char *a){
int k=0;
TrieNode *p=root;
while(a[k]){
if(!p->next[a[k]-'a'])p->next[a[k]-'a']=new TrieNode;
p=p->next[a[k++]-'a'];
}
++p->num;
} void Build_AC(){
int k=0;
TrieNode *p=root,*next;
queue<TrieNode *>q;
q.push(p);
while(!q.empty()){//一层一层的构造fail
p=q.front();
q.pop();
for(int i=0;i<26;++i){
if(p->next[i]){
next=p->fail;
while(next){
if(next->next[i]){p->next[i]->fail=next->next[i];break;}
next=next->fail;
}
if(!next)p->next[i]->fail=root;
q.push(p->next[i]);
}
}
}
} int SearchTrie(char *a){
int k=0,sum=0;
TrieNode *p=root,*next;
while(a[k]){
while(!p->next[a[k]-'a'] && p != root)p=p->fail;
p=p->next[a[k++]-'a'];
if(!p)p=root;
next=p;
while(next != root && next->num != -1){//不能用0来判断是否已经寻找过
sum+=next->num;
next->num=-1;//这里注意一定要赋值为负数,不能为0
next=next->fail;
}
}
return sum;
} void Free(TrieNode *p){
for(int i=0;i<26;++i)if(p->next[i])Free(p->next[i]);
delete p;
} int main(){
int T,n;
cin>>T;
while(T--){
root=new TrieNode;
cin>>n;
for(int i=0;i<n;++i){
cin>>a;
InsertNode(a);//插入单词
}
Build_AC();//构造失败指针,也就是建立AC自动机的精髓
cin>>b;
cout<<SearchTrie(b)<<endl;//查询含有的单词数量
Free(root);
}
return 0;
}
/*
10
2
abcdef
bcd
abcdef
1
h
hhhhh
5
bhea
her
he
h
ha
bhera
5
bhea
her
he
h
ha
bhera
*/
hdu2222之AC自动机入门的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu2222 ac自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机入门
Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...
- HDU 2222 Keywords Search(AC自动机入门)
题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu-2222(ac自动机模板)
题意:给你一个长度为n的单词表,一个文本串,问你这个文本串中出现了单词表中多少个单词: 解题思路:ac自动机的模板题,可以直接当模板用: 代码: #include<iostream> #i ...
- hdu 1277 AC自动机入门(指针版和数组版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
随机推荐
- gridview外边距
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区. 您需要 登录 才可以下载或查看,没有帐号?注册 x 本帖最后由 同舟 于 2013-9-30 11:44 编辑 最新项目需要个单行显示功能键 ...
- 第二章 andrid studio创建项目
原文 http://blog.csdn.net/zhanghefu/article/details/9326735 第二章 andrid studio创建项目 第二章 andrid studio创建项 ...
- Matlab.NET混编技巧之——找出Matlab内置函数
原文 http://www.cnblogs.com/asxinyu/p/3295309.html Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯 定不难.反之,有时 ...
- GDB单步调试程序
linux下gdb单步调试 用 GDB 调试程序 GDB 概述———— GDB 是 GNU开源组织发布的一个强大的 UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像 VC. BCB等 ...
- MFC网页
写网页, 选择MFC,MFC应用程序,其他默认,单击确定 项目类型,选Offce,其他默认,单击下一步 默认,单击下一步 文件拓展名,输入html,其他默认,单击下一步 数据库支持,默认,单击下一步 ...
- The encryption certificate of the relying party trust identified by thumbprint is not valid
CRM2013部署完ADFS后通过url在浏览器中訪问測试是否成功,成功进入登陆界面但在登陆界面输入username和password后始终报身份验证失败,系统中的报错信息例如以下:Microsoft ...
- dog-fooding-our-api-authentication
Dog-fooding our API - Authentication http://blog.mirajavora.com/authenticate-web-api-using-access-to ...
- CSS 根据数据显示样式
在低版本IE时代,我们想让数据根据其值显示不同的样式可能需要直接从服务器端输出时为不同的数据添加相应的class.但现在,通过属性选择器+伪元素+属性选择符,这三个东西混合使用就可以让数据根据其值以不 ...
- TCP协议三次握手过程分析
TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位,有6种标 ...
- c++中自增(++)和自减(--)操作符
自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...