题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 65272    Accepted Submission(s): 21782

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

题解:

AC自动机的模板题。模板来自kuangbin, 在此特别鸣谢!

代码如下:

 #include<bits/stdc++.h>
#define rep(i,a,n) for(int (i) = a; (i)<=(n); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e5+; struct Trie //封装在一个结构体中
{
int next[MAXN][], fail[MAXN], end[MAXN];
int root, L; int newnode()
{
for(int i = ; i<; i++)
next[L][i] = -;
end[L++] = ;
return L-;
} void init()
{
L = ;
root = newnode();
} void insert(char buf[]) //往Trie树中插入单词
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-'a'] == -)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now]++;
} void build() //构建fail指针
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<; i++)
{
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i<; i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(char buf[]) //将字符串与Trie树进行匹配
{
int len = strlen(buf);
int now = root;
int res = ;
for(int i = ; i<len; i++)
{
now = next[now][buf[i]-'a'];
int tmp = now;
while(tmp != root)
{
res += end[tmp];
end[tmp] = ;
tmp = fail[tmp]; }
}
return res;
}
}; Trie ac;
char buf[MAXN<<];
int main()
{
int T, n;
scanf("%d",&T);
while(T--)
{
ac.init();
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}

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

  1. hdu2222 Keywords Search ac自动机

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

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

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

  3. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  4. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  5. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  6. 【HDU2222】Keywords Search AC自动机

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

  7. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

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

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

  9. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. [原创][FPGA]Quartus实用小技巧(长期更新)

    0. 简介 在使用Quartus软件时,经常会时不时的发现一些小技巧,本文的目的是总结所查阅或者发现到的小技巧,本文长期更新. 1. Quartus中的模板功能 最近在Quartus II的菜单里找到 ...

  2. [笔记][FPGA]有限状态机FSM学习笔记(三)

    0. 简介 在数电FPGA中,FSM是一个重要的部分,藉此可以完成一些复杂算法的硬件实现等.其中有关于FSM的写法按照always块的个数来划分,又分为一段式.两段式.三段式状态机.顾名思义,一段式就 ...

  3. va_list 简介

    原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...

  4. CDOJ 3 BiliBili, ACFun… And More! 模拟

    原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...

  5. Cesium热力图实现

    转自原文 Cesium热力图实现 生成热力图的算法我是用的一个热力图插件 heatmap.js.   heatmap中热力图生成原理: heatmap中首先会根据输入的渐进色参数,在内部生成一个0-2 ...

  6. Android自定义控件之自定义属性(二)

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  7. 浅析 rand7生成rand10 方法 之 思想篇(一)

    [问题描写叙述] rand7是一个能生成1-7的随机数.要求利用rand7生成1-10的随机数. [算法思想] 1.组合数学方法 第1次 1 2 3 4 5 6 7 之中用rand7取一个数 第2次从 ...

  8. CloudStack管理VMware遇到的问题

    话说前段安装了CloudStack并使用它来管理XenServer,这回要用它来管理VMware.虽说之前遇到了大大小小的问题都攻克了,但在VMware这一块还是遇到了一些麻烦. 在创建资源域.加入集 ...

  9. Net is as typeof 运行运算符详解 net 自定义泛型那点事

    Net is as typeof 运行运算符详解   概述 在了解运行运算符的前提我们需要了解什么是RTTI ,在任何一门面向对象的语言中,都有RTTI这个概念(即 运行时). RTTI(Run-Ti ...

  10. mdadm

    http://en.wikipedia.org/wiki/Mdadm mdadm From Wikipedia, the free encyclopedia     mdadm Original au ...