kmp

POJ3461 Qulipo(kmp)

泪湿孤枕 提交于 2020-01-11 13:32:03
传送门 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24124 Accepted: 9679 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e' . He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d

POJ 3461 Oulipo(KMP统计子串出现次数)

最后都变了- 提交于 2020-01-10 15:03:52
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12818 Accepted: 5128 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e' . He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un

【2772】KMP的简单应用 sdutOJ

若如初见. 提交于 2020-01-10 01:56:16
 KMP简单应用 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定两个字符串string1和string2,判断string2是否为string1的子串。 输入 输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。 输出 对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。 示例输入 abc a 123456 45 abc ddd 示例输出 1 4 -1 #include <stdio.h> #include <string.h> #include <stdlib.h> int next[1000010]; char a[1000010],b[1000010]; void get_next() { int i=0; int j=-1; next[0]=-1; int len=strlen(b); while(i<len) { if(j==-1||b[i]==b[j]) { i++; j++; next[i]=j; } else j=next[j]; } } void KMP() { int i=0,j=0

hdu-1711 kmp算法学习

若如初见. 提交于 2019-12-28 15:56:52
看了两篇博客 字符串匹配的KMP算法 【经典算法】——KMP,深入讲解next数组的求解 以下为例题:( Number Sequence HDU - 1711 ) 要求: Given two sequences of numbers : a[1], a[2], … , a[N], and b[1], b[2], … , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], … , a[K + M - 1] = b[M]. If there are more than one K exist, output the smallest one. Input: The first line of input is a number T which indicate the number of cases. Each case contains three lines. The first line is two numbers N and M (1 <= M <= 10000, 1 <= N <= 1000000). The second line contains N integers which

字符串模式匹配之KMP算法的next数组详解与C++实现

↘锁芯ラ 提交于 2019-12-27 17:34:54
相信来看next数组如何求解的童鞋已经对KMP算法是怎么回事有了一定的了解,这里就不再赘述,附上一个链接吧:https://www.cnblogs.com/c-cloud/p/3224788.html,里面对KMP算法有详细的讲解,如果你还不了解KMP算法,可以看看~~。 下面就来讲解不容易理解但又很重要的next数组,相信这是你看过的最容易理解的next数组的讲解了(*^_^*)。 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 一、首先,next数组是什么? 简单来说,假设我们有一个主串S和一个模式串T,并且想知道S是否包含T,如果包含,那么T第一次出现在S中的首字符在什么位置?有一种暴力求法:当S[i]!=T[j]的时候,j回溯到j=0,而i回溯到i=i-j+1,这种方法简答粗暴,但效率低下,时间复杂度的范围是(最好与最坏情况):O(n+m)~O(n*m),其中,n为主串S的长度,m为模式串T的长度; KMP算法

KMP算法

℡╲_俬逩灬. 提交于 2019-12-27 00:02:17
KMP算法 在介绍KMP算法之前,先介绍一下BF算法。 一.BF算法 BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符;若不相等,则比较S的第二个字符和P的第一个字符,依次比较下去,直到得出最后的匹配结果。 举例说明: S: ababcababa P: ababa   BF算法匹配的步骤如下 i=0 i=1 i=2 i=3 i=4 第一趟: a babcababa 第二趟:a b abcababa 第三趟:ab a bcababa 第四趟:aba b cababa 第五趟:abab c ababa a ba ba a b aba ab a ba aba b a abab a j=0 j=1 j=2 j=3 j=4(i和j回溯) i=1 i=2 i=3 i=4 i=3 第六趟:a b abcababa 第七趟:ab a bcababa 第八趟:aba b cababa 第九趟:abab c ababa 第十趟:aba b cababa a baba a baba a b aba ab a ba a baba j=0 j=0 j=1 j=2(i和j回溯) j=0 i=4 i=5 i=6 i=7 i=8 第十一趟:abab c ababa 第十二趟:ababc a baba 第十三趟

BF算法与KMP算法

时光总嘲笑我的痴心妄想 提交于 2019-12-26 11:14:43
BF(Brute Force)算法 是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果。 BF算法实现: 1 int BF(char S[],char T[],int pos) 2 {//c从第pos位开始搜索匹配 3 int i=pos,j=0; 4 while(S[i+j]!='\0'&&T[j]!='\0') 5 { 6 if(S[i+j]==T[j]) 7 j++; 8 else 9 { 10 i++; 11 j=0; 12 } 13 } 14 if(T[j]=='\0') 15 return i+1; 16 else 17 return -1; 18 } BF算法比较直接,是一种蛮力算法,该算法最坏情况下要进行M*(N-M+1)次比较,时间复杂度为O(M*N),下面来看一个效率非常高的字符串匹配算法,即KMP算法。KMP算法完成的任务是:给定两个字符串S和T,长度分别为n和m,判断f是否在S中出现,如果出现则返回出现的位置。常规方法是遍历a的每一个位置,然后从该位置开始和b进行匹配,但是这种方法的复杂度是O(nm)。kmp算法通过一个O(m)的预处理,使匹配的复杂度降为O(n+m)

POJ3461–Oulipo(KMP)

我们两清 提交于 2019-12-24 23:19:56
题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #include<string> #include<cstdio> using namespace std; #define MAXN 10005 char T[MAXN*100],P[MAXN]; int f[MAXN]; void getFail() { int m=strlen(P); f[0]=f[1]=0; for(int i=1; i<m; i++) { int j=f[i]; while(j&&P[j]!=P[i]) j=f[j]; f[i+1]=P[j]==P[i]?j+1:0; } } int find() { int n=strlen(T),m=strlen(P); getFail(); int j=0,ans=0; for(int i=0; i<n; i++) { while(j&&P[j]!=T[i]) j=f[j]; if(P[j]==T[i]) j++; if(j==m) ans++; } return ans; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%s%s",P,T); printf("%d\n",find())

DS串应用--KMP算法

ぐ巨炮叔叔 提交于 2019-12-18 02:03:11
DS串应用–KMP算法 题目描述 学习KMP算法,给出主串和模式串,求模式串在主串的位置 算法框架如下,仅供参考 输入 第一个输入t,表示有t个实例 第二行输入第1个实例的主串,第三行输入第1个实例的模式串 以此类推 输出 第一行输出第1个实例的模式串的next值 第二行输出第1个实例的匹配位置,位置从1开始计算,如果匹配成功输出位置,匹配失败输出0 以此类推 样例输入 3 qwertyuiop tyu aabbccdd ccc aaaabababac abac 样例输出 -1 0 0 5 -1 0 1 0 -1 0 0 1 8 #include <iostream> #include <string> using namespace std; class myString{ string S; string T; int *next; void getNext(); public: myString(); ~myString(); int KMP(); void output(); }; myString::myString() { cin>>S>>T; next = new int[T.size()]; } myString::~myString() { delete []next; } int myString::KMP() { int i,j; for(i=0,j=0;i

Luogu P2375 [NOI2014]动物园 KMP

时间秒杀一切 提交于 2019-12-16 15:25:47
好,暴力能拿$50pts\space qwq$ 暴力的思路就是一直跳$nxt[j]$,直到它的长度小于串的一半,然后开始计数,当然要接着跳$nxt[j]$ 正解:考虑没有长度要求的(不要求不重合)公共前后缀的数目,显然$ans[i]=ans[j]+1$相当于$i$比$j$是多了$i$它本身。 所以求解时模仿$kmp$的过程,$num[i]$就是一直跳$nxt[j]$,然后直到$j<=\frac{i}{2}$,$num[i]=ans[j]$ 注意,此处模仿$kmp$的原因是,能够避免跳过一些冗余的$nxt[j]$($nxt[j]$过大的情况),对于下一次匹配,上一次的$j$就是上一次的小于等于$\frac{i}{2}$的最长公共前后缀的长度,如果还能匹配就直接匹配,过长了再跳一下$nxt[j]$,否则就一直跳$nxt[j]$,直到匹配。 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstdlib> #include<vector> #include<queue> #include<map> #include<set> #define ull unsigned long long #define ll