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. 

InputFirst 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. 
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3
AC自动机:利用ttie树节省空间,利用kmp相似的原理构建fail指针进行匹配
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 26
#define L 31
#define INF 1000000009
#define eps 0.00000001
struct node
{
node()
{
cnt = ;
fail = NULL;
for (int i = ; i < MAXN; i++)
next[i] = NULL;
}
int cnt;
struct node* next[MAXN];
struct node* fail;
};
typedef struct node* Tree;
void insert(Tree T, const char* str)
{
int p = ;
Tree tmp = T;
while (str[p] != '\0')
{
int k = str[p] - 'a';
if (tmp->next[k] == NULL)
{
tmp->next[k] = new node();
}
tmp = tmp->next[k];
p++;
}
tmp->cnt++;
}
void build_ac(Tree root)
{
root->fail = NULL;
queue<Tree> q;
q.push(root);
while (!q.empty())
{
Tree tmp = q.front();
q.pop();
Tree p = NULL;
for (int i = ; i < MAXN; i++)
{
if (tmp->next[i] != NULL)
{
if (tmp == root)
tmp->next[i]->fail = root;
else
{
p = tmp->fail;
while (p != NULL)
{
if (p->next[i] != NULL)
{
tmp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL) tmp->next[i]->fail = root;
}
q.push(tmp->next[i]);
}
}
}
}
int query(Tree root,const char* str)
{
int i = ;
int count = ;
int l = strlen(str);
Tree tmp = root;
int index;
while (str[i])
{
index = str[i] - 'a';
while (tmp->next[index] == NULL && tmp != root)
tmp = tmp->fail;
tmp = tmp->next[index];
if (tmp == NULL) tmp = root;
Tree p = tmp;
while (p != root)
{
count += p->cnt;
p->cnt = ;
p = p->fail;
}
i++;
}
return count;
} void del(Tree root)
{
for (int i = ; i < MAXN; i++)
if (root->next[i] != NULL)
del(root->next[i]);
delete root;
}
char key[], s[];
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
Tree root = new node();
root->cnt = ;
root->fail = NULL;
for (int i = ; i < MAXN; i++)
root->next[i] = NULL;
for (int i = ; i < n; i++)
{
scanf("%s", key);
insert(root, key);
}
build_ac(root);
scanf("%s", s);
printf("%d\n", query(root, s));
del(root);
}
}

Keywords Search AC自动机的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  3. HDU2222 Keywords Search [AC自动机模板]

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

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

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

  5. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

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

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

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

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

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

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

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

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

随机推荐

  1. [Usaco2005 oct]Flying Right 飞行航班

    Description 为了表示不能输给人类,农场的奶牛们决定成立一家航空公司.她们计划每天早晨,从密歇根湖湖岸的最北端飞向最南端,晚上从最南端飞往最北端.在旅途中,航空公司可以安排飞机停在某些机场. ...

  2. Visual C++ Windows 桌面应用程序样例(摘抄)

    //================================== //Windows应用程序框架结构(例子) //参考:<Visual C++宝典>陈国建等编著 //======= ...

  3. 19 C#循环语句的跳过和中断 continue和break

    在C#的循环语句中,有的时候我们希望跳过其中某个循环,有时我们希望当某个条件满足时,直接终止整个循环.C#为我们提供了 continue;和break;语句. continue和break的用法一样, ...

  4. Font Awesome 图标使用总结

    参考 http://fontawesome.dashgame.com/ 1 大图标递进  fa-lg (33%递增).fa-2x. fa-3x.fa-4x,或者 fa-5x  2 固定宽度  fa-f ...

  5. Code Kata:大整数四则运算—乘法 javascript实现

    上周练习了加减法,今天练习大整数的乘法运算. 采取的方式同样为竖式计算,每一位相乘后相加. 乘法函数: 异符号相乘时结果为负数,0乘任何数都为0 需要调用加法函数 因为输入输出的为字符串,需要去除字符 ...

  6. Vue.js——router-link阻止click事件

    router-link 只能单纯做路由跳转 https://segmentfault.com/q/1010000007896386

  7. 带有空格或tab的字符串的判断

    class test { public static void main(String[] args) { String a = " "; //带有空格的字符串 if ( a.is ...

  8. C_动态库|静态库

    动态库 动态链接库简称DLL,同时以.dll 为后缀,主要用于提供代码和数据 dll 并不是所有数据都能被访问到,必须要进行导出 动态链接库在内存中始终只保存了一份数据,起到了节约内存的作用 生成动态 ...

  9. JMeter在linux上分布式压测环境配置(一)

    环境配置 一.在Linux服务器先安装SDK 1.先从官网下载jdk1.8.0_131.tar.gz,l(linux版本,32位,64位根据系统来判断) 2.在/usr/目录下创建java文件夹,(当 ...

  10. 09js、MySQL相关

    09js.MySQL相关-2018/07/19 1.js的dom 理解一下文档对象模型:html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改;在dom树当中 一切皆 ...