【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页留下了这个问题 ...
随机推荐
- hdu - 2667 Proving Equivalences(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=2767 求至少添加多少条边才能变成强连通分量.统计入度为0的点和出度为0的点,取最大值即可. #include & ...
- 树莓派静态IP配置
一.设置隐藏无线静态ip地址 1.用命令产生wpa_psk:wpa_passphrase “MT-WiFi” “@boozhong.com” 2.sudo nano /etc/network/inte ...
- Redhat 5 无法安装elfutils-libelf-devel-0.137问题
http://whr25.blog.sohu.com/263584338.html 问题: RHEL5.5安装oracle11gR2的时候需要安装elfutils-libelf-devel-0.137 ...
- 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别
启动报错: 2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: '127.0.0.1:50 ...
- 【paddle学习】词向量
http://spaces.ac.cn/archives/4122/ 关于词向量讲的很好 上边的形式表明,这是一个以2x6的one hot矩阵的为输入.中间层节点数为3的全连接神经网络层,但你看右 ...
- 《C++ Primer Plus》学习笔记9
<C++ Primer Plus>学习笔记9 第15章 友元.异常和其他 <<<<<<<<<<<<<<& ...
- 设计模式C++实现——工厂方法模式
模式定义: 工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个. 工厂方法让类把实例化推迟到子类. 模式结构: Creator是一个类,它实现了全部操纵产品的方法,但不实现工厂方法 ...
- 【转载】分布式RPC框架性能大比拼
dubbo.motan.rpcx.gRPC.thrift的性能比较 Dubbo 是阿里巴巴公司开源的一个Java高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 ...
- WebService Get/Post/Soap 方式请求
import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; im ...
- 基于bootstrap_网站汇总页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...