hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 42838 Accepted Submission(s): 13488
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.
1
5
she
he
say
shr
her
yasherhs
3
给出n个字符串和一个模式串 问在模式串中出现了多少个给出的字符串
ac自己主动机模板题
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char str1[55],str2[1111111];
struct node{
node *fail,*next[26];
int ed;
node(){
ed=0;
fail=NULL;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*q[511111],*root;
void tree_insert(char *str){
node *p=root;
int l=strlen(str);
int id;
for(int i=0;i<l;i++){
id=str[i]-'a';
if(p->next[id]==NULL){
p->next[id]=new node();
}
p=p->next[id];
}
p->ed++;
}
void build_ac_automachine(){
int i;
node *temp,*p;
root->fail=NULL;
int fr,ed;
fr=ed=0;
q[ed++]=root;
while(fr<ed){
temp=q[fr++];
for(i=0;i<26;i++){
if(temp->next[i]!=NULL){
if(temp==root)
temp->next[i]->fail=root;
else {
p=temp->fail;
while(p){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;
}
q[ed++]=temp->next[i];
}
}
}
}
int query(char *str){
int cnt=0;
int l=strlen(str);
node *p=root;
for(int i=0;i<l;i++){
int id=str[i]-'a';
while(p->next[id]==NULL&&p!=root)
p=p->fail;
p=p->next[id];
if(p==NULL)
p=root;
node *temp=p;
while(temp!=root&&temp->ed!=-1){
cnt+=temp->ed;
temp->ed=-1;
temp=temp->fail;
}
}
return cnt;
}
int main(){
int t,n;
scanf("%d",&t);
while(t--){
root=new node();
scanf("%d",&n);
while(n--){
scanf("%s",str1);
tree_insert(str1);
}
scanf("%s",str2);
build_ac_automachine();
printf("%d\n",query(str2));
}
return 0;
}
hdu 2222 Keywords Search ac自己主动机的更多相关文章
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
随机推荐
- 控制器view加载
出自李明杰讲课视频
- C# C/S系统开发平台版本区别
各版本功能区别 C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framework-4.0.ht ...
- python的py文件打包成exe
一.首先需要安装Pyinstaller-- 使用pip来安装模块 (我电脑上装的是python的一个编译环境Anaconda,如果电脑上装的是python自带的IDE的话,就直接进入python的安装 ...
- 【转】mysqldump
原文地址:http://blog.chinaunix.net/uid-16844903-id-3411118.html 导出 导出全库备份到本地的目录 mysqldump -u$USER -p$PAS ...
- PDF转图片 C# with Adobe API
PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.
- c++的四种强制类型转换
http://hb.qq.com/a/20110722/001452.htm ...... C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a ...
- [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】
题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...
- 如何解决 Java 安全问题?
如何解决 Java 安全问题,目前的应对策略都十分笨拙,往往适得其反.幸运的是,有一种新的方法可以将安全机制嵌入 Java 执行平台--或者更具体地说,嵌入 Java 虚拟机中,进而规避一些「Big ...
- JavaService wrapper
http://my.oschina.net/yjwxh/blog/260835 http://blog.chinaunix.net/uid-664509-id-3398193.html http:// ...
- RabbitMQ安装简单过程
找到一本ACTION IN RABBITMQ,仔细看.现在先安装起来.. 参考主要的URL,包括安装,用户管理,权限管理.我用的都是最新版本. http://my.oschina.net/indest ...