题目链接

先整理一发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. Jquery库自带的动画效果方法记录

    1.显示和隐藏hide()和show() <script type="text/javascript">             $(function() {      ...

  2. BF算法(朴素的模式匹配算法)

    #include <stdio.h> #include <stdlib.h> int Index_BF(char S[],char T[])//s为目标串(长串),t为模式串( ...

  3. visual assist常用快捷键

    转自http://my.oschina.net/u/211101/blog/127822 一些打开啊新建就不说了…… //先来个我自己最喜欢的,经常不用,老忘记,以前eclipse最喜欢这个快捷键了 ...

  4. 使用html,JavaScript,ajax写一个小型实例

    //1.创建受捐单位数组 var arrOrgData = [     { "Id": 1, "OrgName": "红十字会" },    ...

  5. java中数组与List相互转换的方法

    1.List转换成为数组.(这里的List是实体是ArrayList) 调用ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...

  6. oracle中sql查询语句的执行顺序

    查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...

  7. PHP框架学习之Laravel基本功能

    Laravel5的功能不是一般的多(路由,中间件,blade模版...),单是一个路由功能就让哥很蛋疼,虽说路由使用起来变得很灵活,但真他妹的有点变态了.这里是我照着Laravel5官方文档研究的成果 ...

  8. PHP 数组和对象的相互转化

    对象和数组的相互转化在开发中也是很常见,一般不是多维的情况下直接(array)和(object)就可搞定了,多维的话,遍历下也就可以了: 1 <?php 2 class test 3 { 4 p ...

  9. div模拟textarea以实现高度自适应实例页面

    作为多行文本域功能来讲,textarea满足了我们大部分的需求.然而,textarea有一个不足就是不能像普通div标签一样高度可以跟随内容自适应.textarea总是很自信地显摆它的滚动条,高度固执 ...

  10. python-凯撒密码

    凯撒密码 简介:凯撒密码(Caesar)是最早的代换密码,对称密码的一种 算法:将每个字母用字母表中它之后的第k(称作位移值)个字母替代 代码: #-*-coding:utf-8-*- __autho ...