Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35930    Accepted Submission(s): 11597

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
 
Author
Wiskey
 
ac自动机:  构造一颗Trie树,像kmp一样构造一个失败指针,进行记录;
代码:
 #define LOCAL
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct Trie
{
struct Trie *fail;
struct Trie *child[];
int tail; //末尾标记
}; void _insert(char *s,Trie *root) //构造一个Trie树
{
Trie *newcur,*cur;
cur=root;
for(int i=;s[i];i++)
{
if(cur->child[s[i]-'a']==NULL)
{
newcur= new Trie ;
for(int j=;j<;j++)
newcur->child[j]=NULL;
newcur->fail=NULL;
newcur->tail=;
cur->child[s[i]-'a']=newcur;
}
cur=cur->child[s[i]-'a'];
}
cur->tail++; //有可能有重复的单词
} //构造失败指针
void ac_fail(Trie * root)
{
queue<Trie*>tree;
Trie *fro,*q;
tree.push(root);
while(!tree.empty())
{
fro=tree.front();
tree.pop();
for(int i=;i<;i++){
if(fro->child[i]!=NULL)
{
if(fro==root)
fro->child[i]->fail=root; //将他的下一个函数的指针的失败指针指向当前指针
else
{
q=fro;
while(q->fail)
{
if(q->fail->child[i]){
fro->child[i]->fail=q->fail->child[i];
break;
}
q=q->fail;
}
if(!q->fail) fro->child[i]->fail=root;
}
tree.push(fro->child[i]);
}
}
}
} int query(char *s,Trie *root)
{
Trie *cur=root,*newcur;
int ans=;
for(int i=;s[i];i++)
{
while(cur->child[s[i]-'a']==NULL&&cur!=root)
cur=cur->fail;
cur=cur->child[s[i]-'a'];
if(cur==NULL) cur=root;
newcur=cur;
while(newcur!=root&&newcur->tail>)
{
ans+=newcur->tail;
newcur->tail=;
newcur=newcur->fail;
}
}
return ans;
}
char s1[];
char t1[]; //目标主串
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int cas,n;
Trie *root;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
root= new Trie;
for(int i=;i<;i++)
root->child[i]=NULL;
root->fail=NULL;
root->tail=;
while(n--)
{
scanf("%s",s1);
_insert(s1,root);
}
ac_fail(root);
scanf("%s",t1);
printf("%d\n",query(t1,root));
}
return ;
}

hdu----(2222)Keywords Search(ac自动机)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  9. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

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

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

随机推荐

  1. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  2. json \u unicode字符串转化 c++

    CString GetUStr(const string & str) { std::string showname = str;//\u6211\u7231\u5317\u4eac\u592 ...

  3. Cheatsheet: 2013 08.20 ~ 08.31

    .NET Protobuf-net: the unofficial manual 5 Common C# Misconceptions What is new in the Mono project ...

  4. 判断浏览器是否为IE内核的最简单的方法

    没啥说的,直接贴代码,算是ie hack了. if (!+[1,]) { alert('is ie'); }

  5. C# 返回Foreach集合

    IEnumerable<DataRow> DetailRows()       {           foreach (DataRow dr in EditData.Tables[tb_ ...

  6. Beaglebone Black - 控制 BBB 板上的 LED 灯

    BBB 的板上有五个 LED 灯,一个电源,四个其他指示灯,usr0 至 usr3 .这次学习是控制 usr0 至 3 让它们亮着,熄灭,闪.算是个 Hello World 实验.非常简单. 需要的材 ...

  7. Linux链接库二(动态库,静态库,库命名规则,建立个没有版本号的软连接文件)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...

  8. python_way day13 sqlalchemy

    sqlalchemy 一对多 多对多 1.一对多 一.#创建表结构 class Host(Base): #所有的子类都继承这个基类 #创建表结构 __tablename__ = 'hosts' id ...

  9. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  10. 抓包工具__Windows

    我常使用的: 1. SoftPerfect Network Protocol Analyzer 2. fiddler . fiddler2 3.