kmp

HDUNumber Sequence(KMP)

妖精的绣舞 提交于 2019-12-08 12:55:39
传送门 题目大意:b在a第一次出现的位置 题解:KMP 代码: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 1000008 #define M 10009 using namespace std; int T; int a[N],b[M]; int nxt[M]; int lena,lenb; void getnext() { memset(nxt,0,sizeof(nxt)); for(int i=2,j=0;i<=lenb;i++) { while(b[i]!=b[j+1]&&j) j=nxt[j]; if(b[i]==b[j+1]) nxt[i]=++j; } } int KMP() { getnext(); // for(int i=0;i<=lenb;i++) cout<<i<<"---"<<nxt[i]<<endl; for(int i=1,k=0;i<=lena;i++) { for(;a[i]!=b[k+1]&&k;k=nxt[k]); if(a[i]==b[k+1])++k; if(k==lenb) return i-lenb+1; } return -1; } int main() { scanf("%d",&T); while(T--)

KMP算法模板

拟墨画扇 提交于 2019-12-07 12:53:24
1.啥是KMP算法? KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普拉特操作(简称KMP算法)。KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是通过一个next()函数实现,函数本身包含了模式串的局部匹配信息。KMP算法的时间复杂度O(m+n)。(源自 百度百科 ) 2.代码 #include<bits/stdc++.h> using namespace std; int nxt[1005]; void GetNext(string p) { int pLen=p.length(); nxt[0]=-1; int k=-1; int j=0; while (j<pLen-1) { if(k==-1||p[j]==p[k]) { ++k; ++j; nxt[j] = k; } else k=nxt[k]; } } int KMP1(string s,string p)//返回位置 { int i=0,j=0,sLen=s.length(),pLen=p.length(); GetNext(p); while(i<sLen&&j<pLen) { if(j==-1||s[i]==p[j]) { i++; j++; } else j=nxt

【算法系列 四】 String

五迷三道 提交于 2019-12-06 21:42:47
1. 字符串循环左移( 九度OJ1362 ),要求时间复杂度O(N),空间复杂度O(1) 这是一道基本的题目,简单说来就是三次翻转 比如:abcdef 左移两位 cdefab 过程: ab 翻转 ba cdef 翻转 fedc 将上面两个翻转后的结果拼接 bafedc 再翻转cdefab得到结果 代码: import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner cin = new Scanner(System.in); int N; String str; while (cin.hasNext()) { str = cin.next(); N = cin.nextInt(); N = N % str.length(); String a = str.substring(0, N); String b = str.substring(N); StringBuffer abuffer = new StringBuffer(a); StringBuffer bbuffer = new StringBuffer(b); StringBuffer areverse =

KMP

被刻印的时光 ゝ 提交于 2019-12-05 19:39:21
简介: KMP算法,适用于模式匹配,即查找模式串P在字符串S内的出现位置,其时间复杂度为O(M+N)。 (题外话:模式匹配问题是算法竞赛的常客,但理解KMP算法具有一定难度,建议先理解BF算法后再理解KMP算法。当然,网上关于KMP算法的讲解很多,这里仅仅只是给出模板。) 模板: 下面给出2种求next数组的方法,其中优化next数组求法虽然使得kmp算法更快,但针对需要输出模式串p的next数组的算法题,往往多数都是要原方法求出next数组。两种方法求出的next数组有什么区别可以通过网上关于KMP算法的讲解了解到。 1 //求出模式串p的next数组 2 void Next(char* p,int *next) 3 { 4 int pLen = strlen(p); 5 next[0] = -1; 6 int k = -1; 7 int j = 0; 8 9 while (j < pLen){ 10 //p[k]表示前缀,p[j]表示后缀 11 if (k == -1 || p[k] == p[j]) { 12 ++k; 13 ++j; 14 next[j] = k; 15 }else{ 16 k = next[k]; 17 } 18 } 19 } 1 //优化next数组求法 2 void Next(char* p, int *next) 3 { 4 int pLen =

KMP总结

試著忘記壹切 提交于 2019-12-05 11:18:27
KMP总结 什么是KMP? KMP算法,又称为模式匹配算法,能够在线性时间内判定字符串 \(A[1\) ~ \(N]\) 是否为字符串 \(B[1\) ~ \(M]\) 的子串,并求出字符串 \(A\) 在字符串 \(B\) 中各次出现的位置。(from 李煜东《算法竞赛进阶指南》) 如何进行KMP? 第一步: \(A\) 串进行自我匹配,构造出 \(next\) 数组 第二步: 通过 \(next\) 数组和 \(B\) 串进行匹配,构造出 \(f\) 数组 什么是 \(next\) 数组? \(next[i]\) 表示 \(A[1..i]\) 的前缀和后缀最大匹配的长度 (且最大匹配不能为 \(A\) 串本身) ,如:(下标从 \(1\) 开始) 对于串 \(ababcab\) 来说, \(next[7]=2\) ;( \(A[1..2]\) 和 \(A[6..7]\) 均为 \(ab\) ) 如何构造 \(next\) 数组? (配合代码食用风味更佳) 代码如下: int next[maxn+5]; void init(char A[],int n){ next[1]=0; for(int i=2,j=0;i<=n;i++){ while(j>0&&A[i]!=A[j+1]) j=next[j]; if(A[i]==A[j+1]) j++; next[i]=j; } }

自主问题--KMP算法

我只是一个虾纸丫 提交于 2019-12-05 09:51:56
再预处理数组时p[i]表示的在模式串前i个字符中 前缀和后缀 最大公共长度 //例如下模式串ababa p[1]=0; p[2]=1; p[3]=2; p[4]=2; p[5]=3; 在kmp时,移动j要用while 输入 char a[10],b[10]; cin>>a+1>>b+1; KMP时判断字符是否相等要+1 while(j>0&&s1[i+1]!=s2[j+1]) j=p[j]; if(s1[i+1]==s2[j+1]) { j++; }if(j==m) { printf("%d\n",i-m+2); j=p[j]; } 要预处理匹配串和模式串的长度。 来源: https://www.cnblogs.com/mzyczly/p/11920387.html

C++ KMP文本匹配

独自空忆成欢 提交于 2019-12-05 02:56:06
代码如下: 环境为VC #include <iostream> #include <algorithm> #include <string> #include <map> #include <vector> #include <cstring> #include <utility> #include <fstream> using namespace std; int* getNext(string p) { int* next = new int[p.length()]; next[0] = -1; //第一个字符不匹配, i++,j++ int j = 0; int k = -1; while (j < (int)p.length() - 1) { if (k == -1 || p[j] == p[k]) { j++; k++; next[j] = k; } else { k = next[k]; } } return next; } // KMP算法 int KMPMatcher(string T, string p) { int i = 0; int j = 0; int* next = getNext(T); while (i < (int)T.length() && j < (int)p.length()) { if (j == -1 || T[i] == p[j])

KMP 算法

北城余情 提交于 2019-12-04 20:16:26
需求:字符串中的模式定位问题 主串:“abcxabcdabxabcdabcdabcy” 模式串:“abcdabcy” // 解法一:暴力破解 // 时间复杂度:O(mn) public static int bf(String ts, String ps) { char[] t = ts.toCharArray(); char[] p = ps.toCharArray(); int i = 0; // 主串 int j = 0; // 模式串 while(i < t.length && j < p.length) { if (t[i] == p[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j == p.length) { return i - j; } else { return -1; } } // 解法二:KMP 算法 // 时间复杂度:O(m+n) // Text: abcxabcdabxabcdabcdabcy // Pattern: abcdabcy // KMP Prefix Array Logic public static int[] getNext(String ps) { char[] p = ps.toCharArray(); int[] next = new int[p.length];

KMP

心已入冬 提交于 2019-12-04 13:15:58
KMP #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std; const int N=1000005; int n,m,p[N]; char s1[N],s2[N]; void pre_deal() { p[1]=0; int j=0; for(int i=1;i<m;i++) { while(j>0&&s2[i+1]!=s2[j+1]) j=p[j]; if(s2[i+1]==s2[j+1]) j++; p[i+1]=j; } return; } void kmp() { int j=0; for(int i=0;i<n;i++) { while(j>0&&s1[i+1]!=s2[j+1]) j=p[j]; if(s1[i+1]==s2[j+1]) j++; if(j==m) { printf("%d\n",i+1-m+1); j=p[j]; } } return; } int main() { cin>>(s1+1)>>(s2+1); n=strlen(s1+1); m=strlen(s2+1); pre_deal(); kmp(); for(int i=1;i<=m;i

kmp

孤者浪人 提交于 2019-12-04 10:42:16
kmp,next带优化 #include<iostream> #include<bits/stdc++.h> using namespace std; void getnext(char p[],int next[]){ next[0]=-1; int i=0,j=-1; int n=strlen(p); while(i<n-1){ if(j==-1||p[i]==p[j]){ i++; j++; if(p[i]!=p[j]) next[i]=j; else next[i]=next[j]; } else j=next[j]; } } int kmp(char s[],char p[],int next[]){ int i=0,j=0; int slen=strlen(s); int plen=strlen(p); getnext(p,next); while(i<slen&&j<plen){ if(j==-1||s[i]==p[j]){ i++; j++; } else j=next[j]; } if(j==plen){ return i-j+1; } else return -1; } int main(){ char s[]="ababxbababcadfdsss"; char p[]="ba"; int next[20]={0}; cout<<kmp(s,p,next)<