【hdu 2222】Keywords Search
【题目链接】
【算法】
此题是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的更多相关文章
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- 【AC自动机】Keywords Search
[题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...
- HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
随机推荐
- [bzoj3709][PA2014]Bohater_贪心
bzoj-3709 PA-2014 Bohater 题目大意:在一款电脑游戏中,你需要打败n只怪物(从1到n编号).为了打败第i只怪物,你需要消耗d[i]点生命值,但怪物死后会掉落血药,使你恢复a[i ...
- HDD磁盘,非4K无以致远
机械硬盘的未来要靠高容量作为依托,在财报中,希捷表示未来18个月内它们将推出14和16TB机械硬盘,而2020年20TB机械硬盘就将诞生.也有资料显示,3.5英寸100TB硬盘大概在2025年就能面世 ...
- Docker资源限制实现——cgroup
摘要 随着Docker技术被越来越多的个人.企业所接受,其用途也越来越广泛.Docker资源管理包含对CPU.内存.IO等资源的限制,但大部分Docker使用者在使用资源管理接口时往往还比较模糊. 本 ...
- flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)
函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...
- 在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat
1.安装JDK 1.1.到官网下载相关的JDK 这里下载的是 jdk-6u23-linux-i586.bin. 下载地址:http://www.oracle.com/technetwork/java/ ...
- 初探STL之算法
算法 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包括头文件<algor ...
- [cocos2d-x]怎样降低cocos2d-x游戏的耗电量?
Cocos2d-x游戏的耗电量一直是个让人头疼的问题,一个简单的三消游戏,玩一会手机就热得发烫,更郁闷的是电池消耗非常快.基本上两个小时就能够把电池耗光. 近期又看到一个帖子.有个老外用cocos2d ...
- 如何卸载centos中自带的Java
首先通过 Java -version 来查看是否已经安装了java 然后通过rpm -qa | grep java 来获得java的版本信息 然后再 用 rpm -e --nodeps [这里依次 ...
- Android开发Tips(3)
欢迎Follow我的GitHub, 关注我的CSDN. 我会介绍关于Android的一些有趣的小知识点. 本文是第三篇, 其余第一篇, 第二篇. imageMogr2/auto-orient/stri ...
- addEventListener event
addEventListener 先看个例子: document.getElementById("myBtn").addEventListener("click&qu ...