算法模板记录 - 前缀函数。 KMP 常用于高效的进行字符串模式匹配。 模板 vector<int> prefixFunction(const string &s) { int m = s.size(); vector<int> pi(m); for (int i = 1, j = 0; i < m; i++) { while (j && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]...
KMP算法———模板 做出KMP字符串匹配算法心情也是好好哒,萌萌哒。 感谢黄学长,感谢栋栋! #include<cstdio> #include<string> #include<iostream> using namespace std; int p[101]; int main() { string a,b; cin>>a>>b; int n=a.length(),m=b.length(); a=" "+a;b=" "+b; int j=0; ...
//By Menteur_Hxy#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<cstring>#include<cmath>#include<ctime>usingnamespacestd;constintMAX=1000005;intnext[MAX<<1]; string s1,s2;intmain(){ cin>>s1;getchar();cin>>s2;intlen1=s1.length();intlen2=s2.length();intl...
KMP算法模板代码 #include<iostream>//头文件 #include<string> using namespace std; int pi[10005]; void prefix_function(string s) { int n=s.length(); for(int i=1;i<n;i++) { int j=pi[i-1]; while(j>0&&s[j]!=s[i]) j=pi[j-1]; if(s[j]==s[i]) j++;...
KMP算法 题目:https://www.luogu.com.cn/problem/P3375 publicstaticvoidkmp_search(String s1, String s2){if(s2.isEmpty()) System.out.println("-1");intn =s1.length();intm =s2.length(); String ss1= " "+s1; String ss2= " "+s2;charstr1[] =ss1.toCharArray();charstr2[] =ss2....
把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题。 #include<stdio.h>#include<string.h>#defineN1000005#defineM10005inta[N],b[M];intnext[M];intn,m;voidsetNext(){inti,j;i=0;j=-1;next[i]=j;while(i<m){if(j==-1||b[i]==b[j]){i++;j++;next[...
namespace KMP{ vector<int> next; void build(const string &pattern){ int n = pattern.length(); next.resize(n + 1); //最后多出一位 for(int i = 0, j = next[0] = -1; i < n; next[++i] = ++j){ //j代表前缀pos, i代表后缀pos while(~j && pattern[i] != pattern[j]) ...
#include<bits/stdc++.h> using namespace std; void getNext(vector<int>& next, string str) { next[0] = 0; int i, j = 0; for (i = 1; i < next.size(); i++) { while (j > 0 && str[j] != str[i]) { j = next[j - 1]; } if (str[j] == str[i]) { j++; ...
int*findNext(string&s){intlen=s.length();int*next=newint[len];next[0]=-1;inti,k;i=0,k=-1;while(i<len-1){while(k>=0&&s[i]!=s[k])k=next[k];i++;k++;next[i]=k;}returnnext;};int*findBetterNext(string&s){intlen=s.length();int*next=newint[len];next[0]=-1;inti...
#include<string.h> #include<iostream> #include<string> char ss[110][110];char ss0[110];char pp[110],ppf[110];int next[110];int main(){ int i,j; int n; int t; int len,len0; int num; int flag; int max; scanf("%d",&t); while(t--)