【题目链接】

点击打开链接

【算法】

此题是AC自动机模板题

AC自动机是很神奇的算法,简单点来说,就是在一棵字典树上进行KMP,它的应用范围很广,非常实用

这篇博客写得很好,推荐阅读 :

http://blog.csdn.net/creatorx/article/details/71100840

【代码】

个人觉得我的代码还是写得不错的,大家可以尝试阅读一下,应该可读性较高.

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std; int T,N,i;
char pattern[],s[][]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = x * + c - '';
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct AC_Automation {
int tot;
struct node {
int next[];
int fail;
int sum;
} trie[];
void clear() {
int i;
for (i = ; i <= tot; i++) {
trie[i].sum = trie[i].fail = ;
memset(trie[i].next,,sizeof(trie[i].next));
}
tot = ;
}
void insert(char *s) {
int i,len,root=,x;
len = strlen(s);
for (i = ; i < len; i++) {
x = s[i] - 'a';
if (!trie[root].next[x]) trie[root].next[x] = ++tot;
root = trie[root].next[x];
}
++trie[root].sum;
}
void rebuild() {
int i,root,tmp;
queue<int> q;
q.push();
trie[].fail = -;
while (!q.empty()) {
root = q.front(); q.pop();
for (i = ; i < ; i++) {
if (trie[root].next[i]) {
if (!root)
trie[trie[root].next[i]].fail = ;
else {
tmp = trie[root].fail;
while (tmp != -) {
if (trie[tmp].next[i]) {
trie[trie[root].next[i]].fail = trie[tmp].next[i];
break;
}
tmp = trie[tmp].fail;
}
if (tmp == -) trie[trie[root].next[i]].fail = ;
}
q.push(trie[root].next[i]);
}
}
}
}
void query(char *s) {
int i,x,len,tmp,p=,ans=;
len = strlen(s);
for (i = ; i < len; i++) {
x = s[i] - 'a';
while ((p != ) && (!trie[p].next[x])) p = trie[p].fail;
p = trie[p].next[x];
tmp = p;
while (tmp) {
if (trie[tmp].sum != ) {
ans += trie[tmp].sum;
trie[tmp].sum = ;
} else break;
tmp = trie[tmp].fail;
}
}
writeln(ans);
}
} ACAM; int main() { read(T);
while (T--) {
read(N);
for (i = ; i <= N; i++) gets(s[i]);
gets(pattern);
ACAM.clear();
for (i = ; i <= N; i++) ACAM.insert(s[i]);
ACAM.rebuild();
ACAM.query(pattern);
} return ; }

【hdu 2222】Keywords Search的更多相关文章

  1. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  2. 【AC自动机】Keywords Search

    [题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定  n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

  4. 【HDU2222】Keywords Search AC自动机

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

  5. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  6. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  7. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  8. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  9. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

随机推荐

  1. 顿悟:Linux是拿来用的,不是拿来折腾的

    Linux是拿来用的,而不是折腾其本身.相信这个道理不少聪明人(实用主义者)都明白,然而总是有那么一群人拿Linux去安装各种发行版.研究Linux命令.配置桌面.美化桌面.研究各种wm/DE.永无止 ...

  2. 二叉树的层次遍历 II

    给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 ...

  3. 9.Java web—JSP内置对象

    容器内置了9大对象,这些对象在jsp页无需实例化,可以直接使用. 分别为request. response .session. application .out. pageContext .confi ...

  4. oracle存储过程中使用字符串拼接

    1.使用拼接符号“||” v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAccount || '' ...

  5. Wide & Deep Learning Model

    Generalized linear models with nonlinear feature transformations (特征工程 + 线性模型) are widely used for l ...

  6. [转] 一句shell命令搞定代码行数统计

    今天面试时,突然被面试官问到怎样用shell命令搞定某个文件夹下java代码行数的统计. 想了一下,基本思路就是找到这个文件夹下面的所有java文件,然后每个文件统计一下代码,外层套个for循环,叠加 ...

  7. 【整理】nand相关

    记录nand相关知识.主要是mtd和ubi 什么是UBI 它是一种flash管理方式 flash是一系列连续的物理擦除块组成的. UBI卷是一系列连续的逻辑擦除块(eraseblock),每一块都能够 ...

  8. Android 特别大的Activity和Fragment的生命周期图

    这么 这么大的图.不做太多解释,哈哈,真的是棒棒的. 代码測试下载:http://download.csdn.net/detail/pcaxb/8906085

  9. UVa 12377 - Number Coding

    题目:一个数能够用一种素数元素的个数表示的形式,43560=23×32×51×112表示成41223: 第一个数是素因子的种类,第二个是每一个素因子的个数递增排列.给你一个这样的形式的串, 问原来的数 ...

  10. iOS 相似淘宝商品详情查看翻页效果的实现

    基本思路: 1.设置一个 UIScrollView 作为视图底层,而且设置分页为两页 2.然后在第一个分页上加入一个 UITableView 而且设置表格可以上提载入(上拉操作即为让视图滚动到下一页) ...