题目链接

先整理一发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. autorelease方法

      基本用法: 1,autorelease 方法会返回对象本身 2,调用完autorelease方法后,对象的计数器不变 2,autorelease 会将对象放到一个自动释放池中 3,当自动释放池被销 ...

  2. java多线程的实现的两种方法

    通过继承Thread类实现 多线程- public class Hello{ public static void main(String args[]){ MyThread tr1 = new My ...

  3. 检索n以内所有素数

    #include <iostream>#include <cmath> const int N=500000;//数组大小 using namespace std; int m ...

  4. strcat()的编写

    1.strcat() #include <windows.h> #include <assert.h> #include <iostream> //strcat() ...

  5. 多个DLL合并,DLL合并到EXE

    1:) 下载 http://download.microsoft.com/download/1/3/4/1347C99E-9DFB-4252-8F6D-A3129A069F79/ILMerge.msi ...

  6. windows下mysql数据库表名大小写不敏感

    最近新入职,领导让做个小功能先练练手.是一个添加分类的功能,有添加和列表,很简单.功能做完后提交,结果在线上出现一个大大的500. 但是我再本地环境下是正常的,我以为可能是php的版本不一致导致的问题 ...

  7. SQL Server 从数据库快照还原数据库

    语法: restore database db_name from database_snapshot  = 'db_snapshot_name'; ------------------------- ...

  8. 算法分析-堆排序 HeapSort 优先级队列

    堆排序的是集合了插入排序的单数组操作,又有归并排序的时间复杂度,完美的结合了2者的优点. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki < ...

  9. python作业day4计算器

    思路: 用循环提取最里面的括号,再进行运算 运算时利用正则表达式寻找相应的运算符 先进行乘除,再进行加减 (参考武sir和金角大王的代码) 流程图: 代码: #!/usr/bin/env python ...

  10. ListView学习小结

    ListView小结 ListView 是Android UI中十分重要的一个组件,在数据的显示上能有着十分灵活的表现,其使用也比较简单,一般包括以下几个要点: 1.  可以通过编写ListActiv ...