【题目链接】

点击打开链接

【算法】

此题是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. Spring 详解(一)------- AOP前序

    目录 1. AOP 简介 2. 示例需求 3. 解决方法一:使用静态代理 4. 解决方法二:使用动态代理 1. AOP 简介 ​ AOP(Aspect Oriented Programming),通常 ...

  2. Hibernate 与Mybatis之比较

    1. hibernate是全自动,而mybatis是半自动.hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成sql.而mybat ...

  3. flask如何设置模板语言Jinjia?如何查看路由视图函数映射?

    首先flask的模板和静态文件命名必须是确定的templates和static pycharm的模板语言设置

  4. add swapspace file on ubuntu.

    https://askubuntu.com/questions/566745/allocate-swap-after-ubuntu-14-04-lts-installation ----------- ...

  5. time machine不备份指定文件夹

    osx中常常会使用timemachine来备份一些文件,timemachine能够使某个文件夹恢复到之前某个时刻的状态,很的方便.但是备份须要空间,特别是有些我们并不想备份一些无关紧要的文件,比方电影 ...

  6. Multiple analytic account plans多辅助核算方案

    定义核算方案     菜单 会计/设置/辅助核算会计/多个方案         点击"创建"按钮         说明 辅助核算方案,输入方案名称     点击"添加一个 ...

  7. 【C语言天天练(九)】动态内存分配

    引言:数组的元素存储于内存中连续的位置上.当一个数组被声明时.它所须要的内存在编译时就被分配. 可是,我们能够使用动态内存分配在执行时为它分配内存. 一块内存的生命周期能够分为四个阶段:分配.初始化. ...

  8. 数据结构与算法之贪心算法 C++实现

    1.基本思路:从问题的某一个初始解触发逐步逼近给定的目标,以尽可能快的求得更好的解. 当达到算法中某一步不能再继续前进时.就停止算法,给出近似值.也就是说贪心算法并不从总体最优考虑,它所作出的选择仅仅 ...

  9. ActiveMQ(三) 转

    package pfs.y2017.m11.mq.activemq.demo03; import javax.jms.Connection; import javax.jms.ConnectionFa ...

  10. C#WinForm窗体监听/拦截操作动作

    C#中的事件也是通过封装系统消息来实现的,如果你在WndProc函数中不处理该消息 那么,它会被交给系统来处理该消息,系统便会通过代理来实现鼠标单击的处理函数,因此你可以通过 WndProc函数来拦截 ...