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 ...
随机推荐
- ext 金额大写
//数字转换成大写金额函数 function atoc(numberValue) { numberValue = numberValue.replace(/,/g,''); numberValue = ...
- VS中无法加入断点进行调试解决方案
原文地址:http://blog.csdn.net/gukesdo/article/details/6535054 [ 1] 以前也遇到过同样的问题,但没有问个为什么,也没有探个毕竟.昨天调试一个DL ...
- tyvj P1135 - 植物大战僵尸 最大权闭合图
P1135 - 植物大战僵尸 From ytt Normal (OI)总时限:10s 内存限制:128MB 代码长度限制:64KB 背景 Background 虽然这么多天了,,虽然 ...
- xcode5时代如何设置Architectures和Valid Architectures
目前ios的指令集有以下几种: 1,armv6,支持的机器iPhone,iPhone2,iPhone3G及对应的iTouch 2,armv7,支持的机器iPhone4,iPhone4S 3,armv7 ...
- Mongo的导出工具mongoexport介绍
需求介绍:将mongodb中的数据以文件的方式导出:json或cvs格式 mongo 提供了mongoexport的工具,可以实现将库中的数据以json或cvs的格式输出到文件中.mongoexpor ...
- Velocity
vm模板 设计原则 让前端来写后端的vm模板,并且前端不需要搭建各种繁杂的后端环境,前后端以 .vm 为沟通桥梁,另外模板的数据源可以在项目开始前前后端约定之后生成JSON文件,从而使两个角色并行开发 ...
- HDU --2665
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU-2522 A simple problem
http://acm.hdu.edu.cn/showproblem.php?pid=2522 学习://除数的运算的应用和算法.除法的本质,如果余数出现重复就表示有循环节 A simple probl ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- java常见错误云集与注意事项
1.安装Eclipse时,尽量jdk不要用jre,jdk包含jre j JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环 ...