Sample Input213 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 1 313 51 2 1 2 3 1 2 3 1 3 2 1 21 2 3 2 1 Sample Output6-1 # include <iostream> # include <cstdio> # include <cstring> using namespace std; ]; ], T[]; //题目中为数字串 int slen, tlen; void getNext…
求模式串在主串中出现的次数Sample Input 3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIANSample Output 130 #include <iostream> #include <cstring> using namespace std; ; int next[N]; char S[N], T[N]; int slen, tlen; void getNext() { int j, k; j = ; k = -; next[] = -;…
求取出现的次数 :  #include<bits/stdc++.h> ; char mo[maxn], str[maxn];///mo为模式串.str为主串 int next[maxn]; inline void GetNext() { , j = -, len = strlen(mo); next[i] = j; while(i < len){ // if(j == -1 || mo[i] == mo[j]) next[++i] = ++j; // else j = next[j];…
Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27028    Accepted Submission(s): 11408 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题意:求模式串在主串中出现的次数,与模式串匹配的子串之间不可重叠. 思路:用kmp算法解决,在匹配更新结果后,重新定位模式串时,不可用j = next[j],应该直接让j定位到模式串开头. code: #include <cstdio> #include <cstring> ; char aa[MAXN]; char bb[MAXN]; int next[MAXN]; int l…
题:http://acm.hdu.edu.cn/showproblem.php?pid=2457 题意:给定n个模式串,给定一个主串,问最替换掉多少个字符使主串不包含模式串或输出“-1”表示没有可行的方案: 分析:给n个模式串建立ac自动机,考虑dp[i][j],表示长度为 i , j 节点变换为主串前 i 个的最小操作数,j节点要转换必须使当前节点为“安全点”,即end[trie[i][j]]==0; dp转化就只要不是自己就要在转移过程中+1,i 位置取min 给下一位i+1 #includ…
Oulipo [题目链接]Oulipo [题目类型]KMP &题意: 给你两个字符串p和s,求出p在s中出现的次数. &题解: kmpC函数就是解题的,其中也就j=nex[j]难理解一些,j=nex[j]就代表对照的开始,如果j=nex[j]的值是4,就代表从第4个位置开始比较,正如这句话上面的那个j++,你都匹配上了,所以就会有j++,之后便会j大于等于匹配串的长度,这就代表找到了一个和匹配串一样的串,所以ans++,之后再去找该从哪个位置开始匹配 [时间复杂度]\(O(n+m)\) &…
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数. 思路:ac自动机做发,val标记每一个病毒串编号,通过print函数统计每一个病毒出现的次数. AC自动机好文章:http://www.cppblog.com/menjitianya/archi…
模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 1000010 int f[N],n,ans,len1,len2; char str1[N],str2[N]; int main() { scanf("%s%s",str2+1,str1+1),len1=strlen(s…
模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 1000010 int ch[N][26],fl[N],head[N],to[N],nxt[N],size[N],pos[N],en[N],n,ans; char str[N]; names…