点击打开链接题目链接

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 42838    Accepted Submission(s): 13488

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
 

给出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自己主动机的更多相关文章

  1. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

  2. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  3. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  4. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  5. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  6. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  7. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  8. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  9. HDU 2222 Keywords Search(AC自己主动机模板题)

    题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...

随机推荐

  1. "git add -A" is equivalent to "git add .; git add -u".

    git add -A stages All git add . stages new and modified, without deleted git add -u stages modified ...

  2. Javascript AMD模块化规范-备用

    AMD是"Asynchronous Module Definition"的缩写,意思是"异步模块定义". 模块定义define(id?, dependencie ...

  3. classpath目录

    WEB-INF/ 是资源目录, 客户端不能直接访问, 这话是没错,不过现在的IDE编译器在编译时会把src下的文件(是文件,不是.java)移到WEB-INF/classes下.不过值得注意的是,sp ...

  4. bzoj 2209: [Jsoi2011]括号序列 splay

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 833  Solved: 392[Submit][Status ...

  5. Displaying 1-16 of 86 results for: deep learning

    Displaying 1-16 of 86 results for: deep learning Deep Learning By Adam Gibson, Josh Patterson Publis ...

  6. 如何得到UBUNTU源代码

    http://www.pleaseguide.me/367/how-to-get-the-source-code-of-ubuntu 在按书作测试,有难点一一解决. Ubuntu's Source c ...

  7. Windows消息对Edit控件的处理

    例如对windows发消息让文本选中. SendMessage(Text1.hwnd,EM_GETSEL,0,-1 ); EC_LEFTMARGIN(&H1) EC_USEFONTINF // ...

  8. 读懂Java中的Socket编程

    Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一.如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的.本文会介绍一下基于TCP/IP的S ...

  9. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  10. MyEclipse 设置字体

    过了好几年,又开始用myeclipse  都忘了,  这里贴图用于记录 1.打开window - preferences . 2.打开General-Appearance-Colors and Fon ...