Keywords Search

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
 #include <iostream>
# include<cstring>
# include<cstdio>
# include<queue>
using namespace std; struct node
{
node *fail; //失败指针
node *next[26]; //Tire每一个节点的26个子节点(最多26个字母)
int count; //是否为该单词的最后一个节点
node(){ //构造函数初始化
fail=NULL;
count=0;
memset(next,NULL,sizeof(next));
}
}*q[1<<6|5];
char keyword[50+5]; //输入的单词
char str[1<<6|5]; //模式串 void insert(char *str, node *root) //建立字典树
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
p->next[id]=new node();
p=p->next[id];
}
p->count++;
} void build_ac_automation(node *root)
{
queue<node *> Q;
int i;
root->fail=NULL;
Q.push(root);
while(!Q.empty())
{
node *temp=Q.front();
Q.pop();
node *p=NULL;
for(i=0;i<26;i++)
{
if(temp->next[i]!=NULL) //temp 为父结点
{
if(temp==root) temp->next[i]->fail=root;
else
{
p=temp->fail; // 思路的关键点,
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail; //p=p->fail也就是p=NULL
}
if(p==NULL) temp->next[i]->fail=root;
}
Q.push(temp->next[i]);
}
}
}
}
int query(node *root)
{
int i=0,cnt=0,len=strlen(str);
node *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
while(p->next[id]==NULL && p!=root) p=p->fail;
p=p->next[id];
p=(p==NULL)?root:p;
node *temp=p;
while(temp!=root && temp->count!=-1)
{
cnt+=temp->count;
temp->count=-1; //表示该单词已经出现过了。防止反复计数
temp=temp->fail; //temp指向e节点的失败指针所指向的节点继续查找
}
}
return cnt;
}
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
node *root=new node();
scanf("%d",&n);
getchar();
while(n--)
{
gets(keyword);
insert(keyword,root);
}
build_ac_automation(root);
scanf("%s",str);
printf("%d\n",query(root));
}
return 0;
}


Keywords Search (ac 自己主动机)的更多相关文章

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

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

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

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

  3. hdu2222--Keywords Search+AC自己主动机模板

    题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...

  4. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

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

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

  6. AC自己主动机

    AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS  这里另一个Kuangbin开的比赛,大家 ...

  7. [POJ 1204]Word Puzzles(Trie树暴搜&amp;AC自己主动机)

    Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...

  8. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  9. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

随机推荐

  1. 大数据时代的 9 大Key-Value存储数据库

    在过去的十年中,计算世界已经改变.现在不仅在大公司,甚至一些小公司也积累了TB量级的数据.各种规模的组织开始有了处理大数据的需求,而目前关系型数据库在可缩放方面几乎已经达到极限. 一个解决方案是使用键 ...

  2. TImage也有OnClick事件,可以当按钮使用,配上合适的图片(背景透明,效果前凸)更是几乎以假乱真

    本质上TImage与TSpeedButton没有什么区别,都是没有句柄的,但都可以执行OnClick事件.有空分析一下.

  3. Oracle 当前时间加减

     当我们用 select sysdate+number from dual ;我们得到的是,当前的时间加上number天后的时间.从这里我们也可以看出,使用这种方式进行时间计算的时候,计算的单位是天, ...

  4. No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC

    I'm learning the Spring Framework, and I'm doing the HelloWeb tutorial on tutorialspoint, and I can' ...

  5. 强大的Http监控工具Fidder

    软件下载:http://fiddler2.com/get-fiddler 软件学习:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728 ...

  6. ASP.NET成员资格与角色管理配置内容

    Web.config中进行配置 以便于连接数据库,使用微软提供的Membership类.·····等   <?xml version="1.0" encoding=" ...

  7. Visual Studio 必备神器---转

    会使用工具是人类文明的一大进步,今天敏捷大行其道,好的工具可以大大的提高生产力,这里说的工具都是VS平台上的扩展工具,一些机械的部分可以交给工具去处理,自己多关注其他部分.下面分享下我觉得不错的工具, ...

  8. 百度地图SDK for Android v2.1.2全新发布

    2013年6月20日 Android SDK:V2.1.2产品上线 新增: 自定义指南针位置(类:MapController,方法:setCompassMargin) 自定义当前位置图标(类:MyLo ...

  9. EL表达式(1)

    JSP页面中支持使用EL表达式,EL全名为Expression Language.EL表达式的主要作用有: ① 获取数据: ② 执行运算: ③ 使用EL表达式的11大隐式对象: ④ 调用Java方法. ...

  10. 控件编写:增强 TMEMO (一)(增加对WM_HSCROLL消息的处理)

    相信没有什么人对 MEMO 陌生了吧.尽管其组件的功能不错.但是,对它进行一些功能的改进,可以更好的使用. 有的时候,我们想要知道,当前的坐标是什么?甚至,想要在 滚动条滚动时触发一些事件. 但,TM ...