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 ...
随机推荐
- php生成员工编号,产品编号
由于某些原因需要获取数据库最大的id值.所以出现了这段php 获取数据库最大的id代码了.这里面的max(id) 这里面的id 就是要获取最大的id了.如果是别的字段请填写为其他字段 获取数据库中最大 ...
- VC下Debug和Release区别
整理日: 2015年3月23日 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下又无法进行调试,于是只能采用printf方式逐步定位到 ...
- 玩转C++运算符重载
运算符重载语法:返回值类型 operator运算符(参数列表) { 代码逻辑... } C++中的运算符重载是通过函数来实现的,可以将重载的运算符看作是类成的一个成员函数,向普通函数一样调用.如重 ...
- ifstream文件尾最后一行读两次
看下面一段代码: ifstream m_fileConfig; string str; m_fileConfig.open(FILE_OPERATORS, ios::out ...
- 《Effective C++》条款26 防卫潜伏的ambiguity模棱两可的状态
每个人都有思想.有些人相信自由经济学,有些人相信来生.有些人甚至相信COBOL是一种真正的程序设计语言.C++也有一种思想:它认为潜在的二义性不是一种错误.ambiguity 这是潜在二义性的一个例子 ...
- Light OJ 1033 - Generating Palindromes(区间DP)
题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ============================================================= ...
- Oracle自动增长的序列号
首先建立序列: Create sequence user_id Increment Start Maxvalue .0E28 Minvalue nocycle Cache Noorder; 然后建立触 ...
- Delphi 用Web App Debugger简单调试ISAPI 转
用Web App Debugger简单调试ISAPI 以isapi为例: 1.新建一个project,用isapi/nsapi: 2.remove这个project中所有的unit: 3.加进你用 ...
- 【转】 各种 基于Unity3d 引擎的Android游戏优化 (drawcall)
合并纹理,减少贴图数量,合并网格,ui上减少不必要的层级叠加关系等 1. 更新不透明贴图的压缩格式为ETC 4bit,因为android市场的手机中的GPU有多种,每家的GPU支持不同的压缩格式 ...
- JavaScript XML 兼容处理,序列化和反序列化以及回调事件
浏览器中XML DOM的支持 IE中通过ActiveXObject实现了XML的支持,存在一下几个版本:Microsoft.XmlDom,MSXML2.DOMDocument,MSXML2.DOMDo ...