题目链接

先整理一发ac自动机模板..

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 5e5+5;
struct AC
{
int nxt[maxn][26], fail[maxn], end[maxn];
int root, len;
int node() {
mem1(nxt[len]);
end[len++] = 0;
return len-1;
}
void init() {
len = 0;
root = node();
}
void insert(char str[]) {
int l = strlen(str);
int now = root;
for(int i = 0; i < l; i++) {
if(nxt[now][str[i]-'a'] == -1) {
nxt[now][str[i]-'a'] = node();
}
now = nxt[now][str[i]-'a'];
}
end[now]++;
}
void build() {
queue <int> q;
fail[root] = root;
for(int i = 0; i < 26; i++) {
if(nxt[root][i] == -1) {
nxt[root][i] = root;
} else {
fail[nxt[root][i]] = root;
q.push(nxt[root][i]);
}
}
while(!q.empty()) {
int now = q.front(); q.pop();
for(int i = 0; i < 26; i++) {
if(nxt[now][i] == -1) {
nxt[now][i] = nxt[fail[now]][i];
} else {
fail[nxt[now][i]] = nxt[fail[now]][i];
q.push(nxt[now][i]);
}
}
}
}
int query(char str[]) {
int l = strlen(str);
int now = root, ans = 0;
for(int i = 0; i < l; i ++) {
now = nxt[now][str[i]-'a'];
int tmp = now;
while(tmp != root) {
ans += end[tmp];
end[tmp] = 0;
tmp = fail[tmp];
}
}
return ans;
}
}ac;
char s[1000005];
int main()
{
int t, n;
cin>>t;
while(t--) {
ac.init();
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
printf("%d\n", ac.query(s));
}
return 0;
}

hdu 2222 Keywords Search ac自动机模板的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  2. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

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

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  7. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

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

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

  9. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

随机推荐

  1. global变量

    在函数体内定义的global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用, $global $a; $a=123; function f() { echo $a; //错 ...

  2. juce AsyncUpdaterMessage 分析

    这个类同样是基于 CallbackMessage, 主要目的是为了在主线程中进行回调,只不过在收到消息的时候进行检测,检测消息发送对象是否已经删除,如果消息发送对象已经没了.消息回调最终调用了调用者的 ...

  3. BigDecimal类对象的使用详解

    双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更小的数进行运算和处理.Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行 ...

  4. Android WebView 小结

    1.设置网页的缩放比例的时候,可以使用以下属性: mWebview.setInitialScale(25);//为25%,最小缩放等级 WebSettings setting = mWebview.g ...

  5. 设置php在apache下加载ini配置文件路径,~和curl扩展无法加载的问题

    php以模块的方式加载到apache的时候,php配置文件目录为C:windows.这不合理,应该选择php本身目录的配置文件加载,可以在apache的httpd.conf配置文件里设置PHPIniD ...

  6. dot.js教程文档api

    dot.js是一个短小精悍的js模板引擎,压缩版仅有4K大小,最近使用dot的时候整理出这个dot.js教程文档,其实称不上什么教程,只是对dot.js的介绍和实例,希望能帮助到一部分需要的人. 使用 ...

  7. 利用js制作异步验证ajax方法()

    如何利用js写ajax异步验证.代码如下: window.onload = function(){ var name = document.getElementById('register-name- ...

  8. mysql 查询重复的(不区分大小写)数据的SQL优化

    在mysql中查询不区分大小写重复的数据,往往会用到子查询,并在子查询中使用upper函数来将条件转化为大写.如: select * from staticcatalogue WHERE UPPER( ...

  9. python 初学笔记 (一)

    初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...

  10. chrome提供的功能

    chrome://chrome-urls/ List of Chrome URLs chrome://accessibility chrome://appcache-internals chrome: ...