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. linux centos7安装mysql

    1.下载并安装官方的 yum repository (新建了mysql文件夹) wget -i -c http://dev.mysql.com/get/mysql57-community-releas ...

  2. jmeter生成时间的函数

    在一个接口测试中,需要提交的请求中要带时间,在看完jmeter帮忙文档,正好总结一下 1.需求 在一个http请求中请求数据要带有时间,如下: 2.实现 突然想到jmeter自带的函数助手好像是可以实 ...

  3. C#手机充值系统开发(基于聚合数据)

    说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: 去聚合数据申请账号-添加手机支付的认证 ...

  4. CSS布局整理

    目录 常用居中方法 水平居中 垂直居中 单列布局 二列&三列布局 float+margin position+margin 圣杯布局(float+负margin) 双飞翼布局(float+负m ...

  5. TypeError: slice indices must be integers or None or have an __index__ method

    由于除法/自动产生的类型是浮点型,因此出现上述错误,修正方法为,将/更改为// roi_gray_lwpCV = gray_lwpCV[y:y + h // 2, x:x + w] # 检出人脸区域后 ...

  6. Jsp页面,结果集分页和sql(top)分页的性能对比

    jsp页面两种分页模式: 第一种: 结果集分页,主要代码见下面: ResultSet rs=stmt.executeQuery(sql); ResultSetMetaData md=rs.getMet ...

  7. 从Element.getElementsByTagName方法说起

    一.getElementsByTagName方法: 我们先看几个解释:1)W3C:getElementsByTagName() 方法可返回带有指定标签名的对象的集合.没有说明返回值的具体类型.2)菜鸟 ...

  8. CAD使用SetXData写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetXData 设置实体的扩展数据,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pXData 扩展数据链表 c#代码实现如下: ...

  9. Java实现打包文件

    把文件打包到压缩包里 public void zip (String... files) throws IOException { //创建文件打包流对象 ZipOutputStream zos = ...

  10. 【转载】dubbo约束的引入(解决eclipse中报错问题)

    在采用分布式系统架构时,我们会经常使用到阿里巴巴的dubbo的分布式框架. 在相关xml配置了dubbo的约束依赖后,即使能上网eclipse.myeclipse等IDE也是无法识别dubbo的相关约 ...