传送门

解题思路

AC自动机,是解决多模匹配问题的算法,是字典树与kmp结合的算法,可以解决许多子串在文本串中出现的次数等信息。关键是实现一个fail指针,是指向更靠上的前缀相同字母,从而可以实现在文本串中跳的操作。

代码

    #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue> using namespace std;
const int MAXN = 1e6+5; int n,ans,cnt; struct Tree{
int vis[30],fail,end;
}AC[MAXN]; inline void build(string S){
int len=S.length();
int now=0;
for(register int i=0;i<len;i++){
if(!AC[now].vis[S[i]-'a'+1])
AC[now].vis[S[i]-'a'+1]=++cnt;
now=AC[now].vis[S[i]-'a'+1];
}
AC[now].end++;
} inline void Get_fail(){
queue<int> Q;
for(register int i=1;i<=26;i++)
if(AC[0].vis[i]){
AC[AC[0].vis[i]].fail=0;
Q.push(AC[0].vis[i]);
}
while(Q.size()){
int x=Q.front();Q.pop();
for(register int i=1;i<=26;i++){
if(AC[x].vis[i]){ //如果这个字母存在,子节点fail指针指向它上一层这个字母的fail所指向的字母,有点难理解,要画图
AC[AC[x].vis[i]].fail=AC[AC[x].fail].vis[i];
Q.push(AC[x].vis[i]);
}
else AC[x].vis[i]=AC[AC[x].fail].vis[i];
}
}
} inline int Query(string S){
int len=S.length();
int now=0,t;
for(register int i=0;i<len;i++){
now=AC[now].vis[S[i]-'a'+1];
for(t=now;t && AC[t].end!=-1;t=AC[t].fail){
ans+=AC[t].end;
AC[t].end=-1;
}
}
return ans;
} int main(){
cin>>n;
for(register int i=1;i<=n;i++){
string s;
cin>>s;
build(s);
}
AC[0].fail=0;
Get_fail();
string s;
cin>>s;
cout<<Query(s)<<endl;
return 0;
}

AC自动机(模板) LUOGU P3808的更多相关文章

  1. (模板)AC自动机模板

    模板1. 给出模式串和文本串,文本串长度小于1e6,模式串长度之和小于1e6,求文本串中有多少模式串出现. 题目链接:https://www.luogu.org/problem/P3808 AC co ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

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

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

  4. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  5. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  6. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  7. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

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

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

  9. KMP与AC自动机模板

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

  10. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

随机推荐

  1. Linux-c 线程锁

      typedef struct _my_mutex { pthread_mutex_t mutex; //互斥锁 pthread_mutexattr_t mta; //互斥锁属性 } my_mute ...

  2. Qt分割线

    方法:使用QFrame QFrame * line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameSh ...

  3. Math: Fibonacci

    https://www.zhihu.com/question/28062458 http://blog.csdn.net/hikean/article/details/9749391 对于Fibona ...

  4. Android开发 从代码里设置Drawable图片不显示的问题

    问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...

  5. [转]成为Java顶尖程序员 ,看这11本书就够了

    “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的,因此从专业性的角度来说,一本好书的价值远超 ...

  6. UVA - 374

    https://vjudge.net/problem/19685/origin 费马小定理优化快速幂 因为加了费马小定理优化,小心2 2 2这种情况,会出现0 0 2,也就是0的0次方,实际答案为0 ...

  7. 关于iosselectjs插件设置同步值的操作实践

    关于移动端选择器的插件选择百度可以搜到很多,之前用过iosselect.js(https://github.com/zhoushengmufc/iosselect)感觉还不错,比mobiscorll. ...

  8. PAT甲级——A1087 All Roads Lead to Rome【30】

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  9. AutoMapper简介

    先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说白了就是一个对象,只不过里边全是数据而已. 为什么要用DTO? 1.DTO更注重数据,对领域对 ...

  10. 使用 MongoDB shell访问MongoDB