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. Django的url解析

    Django中采用正则表达式来匹配所请求的url,这个叫做URLconf,作为MVC中的C(控制器), 这样再调用相应的试图,达到控制器控制试图的显示的效果. 看一下Django的url解析过程 第一 ...

  2. ICMP:Internet控制报文协议

    ICMP:Internet控制报文协议. 是IP层的组成部分.传递差错报文或其他信息. ICMP报文被封装在IP数据报内部: 详细格式例如以下所看到的: 个字段含义例如以下: 8位类型. 表示该ICM ...

  3. 【解决方法】System.IO.FileNotFoundException

    错误日志 See the end of this message for details on invoking just-in-time (JIT) debugging instead of thi ...

  4. 跨平台编程中的宏定义(__LINE__和__DATE__极其有用)

    要用师兄的计算机算东西,无赖那上面是WINDOWS的系统,为了写出能够跨平台的代码,需要在代码中用到宏来选择编译.一种方法是自己在Makefile里面定义好该平台对应的宏.实际上,编译器基本上都会有一 ...

  5. CentOS 6.4 x86_64 安装GCC 4.7.3

    下载gcc-4.7.3.tar.gz, 解压缩 wget ftp://ftp.gnu.org/gnu/gcc/gcc-4.7.3/gcc-4.7.3.tar.gz tar -xzvf gcc-4.7. ...

  6. uvc摄像头代码解析5

    8.初始化uvc控制 8.1 重要结构体 struct uvc_control { //uvc控制 struct uvc_entity *entity; //uvc实体 struct uvc_cont ...

  7. Codility上的问题 (17) PI 2012

    这个题比较简单,给定一个整数数组,对每个元素,求出和它最近比它大的数的距离(下标绝对值),如果没有比它大的数,认为距离是0. 数组元素个数 N [0..50000],数组元素范围[-10^9, +10 ...

  8. java实现小九机器人接口

    package com.iask.webchat.chatmachine; import java.net.URLEncoder; import org.apache.http.HttpEntity; ...

  9. Codeforces Round #311 (Div. 2)

    我仅仅想说还好我没有放弃,还好我坚持下来了. 最终变成蓝名了,或许这对非常多人来说并不算什么.可是对于一个打了这么多场才好不easy加分的人来说,我真的有点激动. 心脏的难受或许有点是由于晚上做题时太 ...

  10. 【MongoDB】在windows平台下搭建mongodb的分片集群(二)

    在上一片博客中我们讲了Mongodb数据库中分片集群的主要原理. 在本篇博客中我们主要讲描写叙述分片集群的搭建过程.配置分片集群主要有两个步骤.第一启动全部须要的mongod和mongos进程. 第二 ...