只是记录一下代码

AC自动机算法的教程请移步这里

还有这里

指针看着懵逼的还可以看一下这里

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 10010
#define maxl 1000100
using namespace std;
int n;
char s[maxl];
struct ac_automation
{
int tot;
struct node
{
node *son[];//记录儿子
int size;//儿子数
node *fail;//失配指针
node ()
{
memset(this,,sizeof(node));
}
};
node *root;
inline void init()
{
root=new node();
}
inline void insert()
{
int l=strlen(s+),i=;
node *now=root;
while(i<=l)
{
if(!now->son[s[i]-'a'])now->son[s[i]-'a']=new node();
now=now->son[s[i]-'a'];
i++;
}
now->size++;
}
inline void build()
{
queue <node*> q;
for(int i=;i<;i++)
{
if(root->son[i])
{
q.push(root->son[i]);
root->son[i]->fail=root;
}
else root->son[i]=root;
}
while(!q.empty())
{
node *x=q.front();
q.pop();
for(int i=;i<;i++)
{
if(x->son[i])
{
x->son[i]->fail=x->fail->son[i];
q.push(x->son[i]);
}
else x->son[i]=x->fail->son[i];
}
}
}
inline int query()
{
node *now=root;
int i=,l=strlen(s+),ans=;
while(i<=l)
{
now=now->son[s[i]-'a'];
for(node *j=now;j!=root&&j->size!=-;j=j->fail)
{
ans+=j->size;
j->size=-;
}
i++;
}
return ans;
}
}ac;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ac.init();//千万别忘了
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s+);
ac.insert();
}
ac.build();
scanf("%s",s+);
printf("%d\n",ac.query());
}
return ;
}

[AC自动机模板]Keywords Search的更多相关文章

  1. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  2. AC自动机(Keywords Search)

    题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...

  3. 【AC自动机】Keywords Search

    [题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定  n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...

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

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

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

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

  6. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  7. POJ2222 Keywords Search AC自动机模板

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

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

随机推荐

  1. CF:Problem 427C - Checkposts强连通 Tarjan算法

    tarjan算法第一题 喷我一脸. ...把手写栈的类型开成了BOOL.一直在找错.. . #include<cstdio> #include<cstring> #includ ...

  2. Write a program that gives count of common characters presented in an array of strings..(or array of

    转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...

  3. Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP

    E. Product Sum   Blake is the boss of Kris, however, this doesn't spoil their friendship. They often ...

  4. jdk、jre、spring、java ee、java se

    1 java se.java ee和java me 这三个是java的标准.java se是根本,java ee建立在java se上,用于server.java me是java se的子集,用于终端 ...

  5. sql compare options

    sql compare project's options Add object existence checks Use DROP and CREATE instead of ALTER Ignor ...

  6. [LeetCode] Construct Binary Tree from Inorder and Pretorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. linux线程相关函数接口

    以下内容转自网络 索引:1.创建线程pthread_create2.等待线程结束pthread_join3.分离线程pthread_detach4.创建线程键pthread_key_create5.删 ...

  8. bzoj 1592: [Usaco2008 Feb]Making the Grade 路面修整【dp】

    因为是单调不降或单调不升,所以所有的bi如果都是ai中出现过的一定不会变差 以递增为例,设f[i][j]为第j段选第i大的高度,预处理出s[i][j]表示选第i大的时,前j个 a与第i大的值的差的绝对 ...

  9. 题解报告:poj 1426 Find The Multiple(bfs、dfs)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  10. ibatis入门教程一

    这几天研究ibatis玩,参考一篇贴子进行安装配置:蓝雪森林 选择这个帖子来跟随配置是因为这个帖子看着比较干净,但是我仍旧在配置得过程中出现了好几个问题,所以我决定在这个帖子的基础上将更多细节加上,做 ...