题目:P3808:https://www.luogu.org/problemnew/show/P3808 P3796:https://www.luogu.org/problemnew/show/P3796 从这里学了下AC自动机:http://www.cnblogs.com/cjyyb/p/7196308.html 我的理解大概就是构建一棵由模式串组成的 Trie 树,然后把文本串一节一节放在上面查找: 失配指针指向的是结尾字母和自己一样的.Trie 树上的其他分支,大约就是在找后缀这样的感觉:…
https://www.luogu.org/problemnew/show/P3966 因为文本串就是字典本身,所以这个和平时的AC自动机不太一样.平时的query要沿着fail树把子树的出现次数依次统计.但是这个的query就是对每个字典里的字符串搞一次.所以就直接按广搜的顺序反过来树形dp统计出子树中的出现次数,直接回答. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN =…
Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; const int mxn=1e7+5; char str[mxn],p[80]; queue<int > q; namespace Trie { int tot,fail[mxn],val[mxn]; int t[mxn][26]; void ins(char *s) { int len=st…
题目链接 //删掉一个单词需要前移一段位置,用链表维护就好了 复杂度O(sum(len)) #include <cstdio> #include <cstring> #define S 26 const int N=1e5+7; int n; char s[N],p[N]; struct AC_Automaton { int cnt,son[N][S],fail[N],pos[N],q[N],dep[N]; bool val[N]; struct List { int l,r; }…
https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using namespace std; #define ll long long struct Trie{ ][],fail[],End[]; int root,L; int newnode(){ /*for(int i=0;i<26;i++) nex[L][i]=-1;*/ End[L++]=; ; } int c…
题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ; ,cnt=,go[xn][],fa[xn],l[xn],siz[xn],tax[xn],a[xn]; char s[xn]; void…
To 洛谷.3373 [模板]线段树2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别表示该数列数字的个数.操作的总个数和模数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含3或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数乘上k 操作2: 格式:…
题目大意: 给定$n$个模式串$p(\sum|p_i|\le10^6)$和一个$t(|t|\le10^6)$,求在$t$中被匹配的$p$的个数. 思路: AC自动机模板题,注意$t$中一个字符可能对应自动机上多个结点,因此需要按照失配指针跳转统计.统计过的结点需要特殊标记,避免重复统计,否则会超时. #include<queue> #include<cstdio> #include<cctype> inline int getint() { register char…
题面 题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. 输入格式: 第一行一个n,表示模式串个数: 下面n行每行一个模式串: 下面一行一个文本串. 输出格式: 一个数表示答案 输入输出样例 输入样例#1: 2 a aa aa 输出样例#1: 2 说明 subtask1[50pts]:∑length(模式串)<=10^6,length(…
题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N]; struct AC_Automaton { int cnt,q[N],val[N],fail[N],las[N],son[N][S]; // struct Node // { // int val,las,fail,son[S]; // Node *son[S];//指针太麻烦了.. // Node()…