KMP字符串匹配
之前写的整理不知道去哪里了,复习一遍吧,下标全部从1开始
KMP可以解决的问题:
-
O ( n + m ) O(n+m) O(n+m)求解串匹配问题,并求出模式串在主串中出现的各个位置
-
通过求解 n e x t next next数组,得到许多有用的信息(题目都很妙)
-
KMP求循环节
1.询问A[1~N]是否是B[1~M]的子串,并求出A在B字符串中各次出现的位置
O(n*m)写法
主串B,模式串A
scanf("%s%s",a+1,b+1);
for(int i=1;i<=strlen(b+1);i++){
for(int j=1;j<=strlen(a+1);j++){
if(b[i]==a[j]){
i++;
if(j==strlen(a+1)){
cout<<i-strlen(a+1)<<" ";
i--;
}
}
else{
i-=(j-1);
break;
}
}
}
O(n+m)hash写法,O(n)预处理主串和模式串的Hash值,枚举主串的每个开始的位置i
查看B[i,i+strlen(A)-1]和A的Hash值是否相同
KMP写法
-
对模式串求 n e x t next next数组,定义 n e x t next next数组, n e x t [ i ] next[i] next[i]表示字符串A中以i结尾的非前缀子串和A的前缀能够匹配的最大长度,(其中非前缀子串就是不能是这个字符串自身。)
n e x t [ i ] = m a x ( j ) , 其 中 j < i 并 且 , A [ 1 j ] = = A [ i − j + 1 , i ] , 不 存 在 j 时 , n e x t [ i ] = 0 next[i]=max(j),其中j<i并且,A[1~j]==A[i-j+1,i],不存在j时,next[i]=0 next[i]=max(j),其中j<i并且,A[1 j]==A[i−j+1,i],不存在j时,next[i]=0
-
如何计算 n e x t next next数组,因为有 n e x t [ 1 ] = 0 next[1]=0 next[1]=0,利用递推的思想,这里和Z函数中Z数组求解类似,因为 Z [ 1 ] = n Z[1]=n Z[1]=n,所以递推求解,假设 n e x t [ 1 ∼ i − 1 ] next[1 \sim i-1] next[1∼i−1]已经计算好了,我们来计算 n e x t [ i ] next[i] next[i],根据定义,我们需要找到最大的 j j j,满足 n e x t next next函数的定义,那么我们称这些 j j j为 n e x t [ i ] next[i] next[i]的所有"候选项",(此处参考蓝书,一看就懂,不过二刷的好处在于,你可以更清楚的知道为什么要这么做了),如果对于每个 i i i,都枚举 i − 1 i-1 i−1个非前缀子串的话,那么时间复杂度也是 O ( n 2 ) O(n^2) O(n2)的。
-
引理,学会反证法. 若 j 0 j_0 j0是 n e x t [ i ] next[i] next[i]的一个候选项,那么比 j 0 j_0 j0小的最大的候选项是 n e x t [ j 0 ] next[j_0] next[j0],也就是[ n e x t [ j 0 ] + 1 ∼ j 0 − 1 next[j_0]+1\sim j_0-1 next[j0]+1∼j0−1]之间没有 n e x t [ i ] next[i] next[i]的候选项.
反证法,有 j ′ j' j′是 n e x t [ i ] next[i] next[i]的一个候选项,假如 n e x t [ j 0 ] < j ′ < j 0 next[j_0]<j'<j_0 next[j0]<j′<j0有, j ′ 应 该 等 于 n e x t [ j 0 ] 与 已 知 矛 盾 j'应该等于next[j_0]与已知矛盾 j′应该等于next[j0]与已知矛盾(简单画图即可,不贴了)
-
根据引理, i 的 候 选 是 : n e x t [ i ] , n e x t [ n e x t [ i ] ] , n e x t [ n e x t [ n e x t [ i ] ] ] , . . . i的候选是:next[i],next[next[i]],next[next[next[i]]],... i的候选是:next[i],next[next[i]],next[next[next[i]]],... 回到之前的问题,当 n e x t [ 1 ∼ i − 1 ] next[1\sim~i-1] next[1∼ i−1]计算完毕之后,因为如果 j j j是 n e x t [ i ] next[i] next[i]的候选项,那么 j − 1 j-1 j−1也一定是 n e x t [ i − 1 ] next[i-1] next[i−1]的候选项(画图可得),所以,计算 n e x t [ i ] 时 next[i]时 next[i]时,使用 n e x t [ i − 1 ] + 1 , n e x t [ n e x t [ i − 1 ] ] + 1 , . . . next[i-1]+1,next[next[i-1]]+1,... next[i−1]+1,next[next[i−1]]+1,...作为 n e x t [ i ] 的 候 选 项 即 可 , 由 此 得 到 了 递 推 关 系 next[i]的候选项即可,由此得到了递推关系 next[i]的候选项即可,由此得到了递推关系
参考蓝书上的代码
next[1]=0;//j表示已经匹配上的长度,因为求next[i],需要next[i-1]+1,next[next[i-1]]+1,...,j就是next[i-1]的候选项
for(int i=2,j=0;i<=n;i++){
//next[i]要求最大,从大到小搜
while(j>0&&a[i]!=a[j+1])j=next[j];//此时j+1 不是next[i]的候选项,
if(a[i]==a[j+1])j++;
next[i]=j;
}
同理求f数组,f[i]表示"B中以i结尾的子串"与"A的前缀"能够匹配的最大长度,那么f[i]==n,就代表
B中有A串,匹配成功。
for(int i=1,j=0;i<=m;i++){
while(j>0&&(j==n||b[i]!=a[j+1]))j=next[j];//j==n,模式串重新匹配
if(b[i]==a[j+1])j++;
f[i]=j;
//if(f[i]==n)A在B中出现
}
Problem P3375 【模板】KMP字符串匹配
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define in insert
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10;
//假设模板串和模板串匹配的过程中第i和第j+1个位置失配,因为
//第i-1和第j是匹配的,求第二个串从j往前走最多多少个格子可以继续进行匹配.
char a[N],b[N];
int ne[N];
int main()
{
scanf("%s%s",a+1,b+1);
int n=strlen(a+1),m=strlen(b+1);
for(int i=2,j=0;i<=m;i++){
while(j>0&&(b[j+1]!=b[i]))j=ne[j];
if(b[j+1]==b[i])j++;
ne[i]=j;
}
for(int i=1,j=0;i<=n;i++){
while(j>0&&(j==m||b[j+1]!=a[i]))j=ne[j];
if(b[j+1]==a[i])j++;
if(j==m){
cout<<i-j+1<<endl;
}
}
for(int i=1;i<=m;i++)
cout<<ne[i]<<" ";
return 0;
}
Problem AcWing 141. 周期(KMP求循环节)
引理:s[1~i]具有长度为len<i的循环节的充要条件是,len能整除i,且s[1~i-len]==s[len+1~i],即i-len是next[i]的候选项。
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+10;
char a[N];
int n,cnt;
int ne[N];
int main()
{
while(cin>>n,n)
{
printf("Test case #%d\n",++cnt);
cin>>a+1;
for(int i=2,j=0;i<=n;i++){
while(j&&a[j+1]!=a[i])j=ne[j];
if(a[j+1]==a[i])j++;
ne[i]=j;
}
for(int i=2;i<=n;i++)
{
int t=i-ne[i];//最小循环元长度;
if(i!=t&&i%t==0)
{
cout<<i<<" "<<i/t<<endl;
}
}
puts("");
}
}
Problem BOI2009Radio Transmission 无线传输
求 给 定 字 符 串 的 最 小 循 环 子 串 串 长 求给定字符串的最小循环子串串长 求给定字符串的最小循环子串串长
// Problem: P4391 [BOI2009]Radio Transmission 无线传输
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4391
// Memory Limit: 125 MB
// Time Limit: 1000 ms
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define in insert
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+10,M=1e9+7;
ll n,m,_;
char p[N*10];
int ne[N*10];
//最小循环串长度?
void solve()
{
cin>>n;cin>>p+1;
int ans=-1;
for(int i=2,j=0;i<=n;i++)
{
while(j&&p[i]!=p[j+1])j=ne[j];
if(p[i]==p[j+1])j++;
ne[i]=j;
//ans=max(ans,ne[i]);不对
}
//cout<<n-ans;
cout<<n-ne[n];
}
int main()
{
solve();
return 0;
}
Problem P3435 [POI2006]OKR-Periods of Words
K M P 好 题 , 求 串 的 最 小 前 缀 后 缀 覆 盖 题 意 挺 难 理 解 的 , 给 一 个 串 S , 求 S 的 一 个 前 缀 子 串 S S ( 不 能 是 S 本 身 ) , 要 求 S 串 是 S S + S S 串 的 前 缀 串 , 求 S 的 每 个 前 缀 子 串 的 最 长 S S 串 , a n s 为 所 有 最 长 S S 串 长 之 和 KMP好题,求串的最小前缀后缀覆盖\\ 题意挺难理解的,给一个串S,求S的一个前缀子串SS(不能是S本身),要求S串是SS+SS串的前缀串,求S的每个\\ 前缀子串的最长SS串,ans为所有最长SS串长之和 KMP好题,求串的最小前缀后缀覆盖题意挺难理解的,给一个串S,求S的一个前缀子串SS(不能是S本身),要求S串是SS+SS串的前缀串,求S的每个前缀子串的最长SS串,ans为所有最长SS串长之和
多写了一个sho数组就T了,蛮离谱的
// Problem: P3435 [POI2006]OKR-Periods of Words
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3435
// Memory Limit: 125 MB
// Time Limit: 1000 ms
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define in insert
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e6+10,M=1e9+7;
ll n,m,_;
char p[N];
ll ne[N];//求串的最短前缀,后缀匹配
ll sho[N];//多写几遍sho竟然直接T了
void solve()
{
cin>>n;cin>>p+1;
for(int i=2,j=0;i<=n;i++)
{
while(j&&p[i]!=p[j+1])j=ne[j];
if(p[i]==p[j+1])j++;
ne[i]=j;
}
ll sum=0; //求每个字符串的最短匹配长度,sum+=n-short(i);
// for(int i=2;i<=n;i++)
// {
// int j=i;
// while(ne[j])sho[i]=ne[j],j=ne[j];
// if(sho[i])
// sum+=i-sho[i];
//
// }
for(int i=2,j=2;i<=n;i++,j=i)
{
while(ne[j])j=ne[j];
if(ne[i])ne[i]=j;
sum+=i-j;
}
cout<<sum<<endl;
}
int main()
{
solve();
return 0;
}
Problem P4824 [USACO15FEB]Censoring S
给 一 个 长 为 n ( n < = 1 0 6 ) 的 串 , 求 主 串 中 删 除 所 有 的 模 式 串 之 后 的 字 符 串 给一个长为n(n<=10^6)的串, 求主串中删除所有的模式串之后的字符串 给一个长为n(n<=106)的串,求主串中删除所有的模式串之后的字符串
88分做法
// Problem: P4824 [USACO15FEB]Censoring S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4824
// Memory Limit: 125 MB
// Time Limit: 1000 ms
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define in insert
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e6+10,M=1e9+7;
ll n,m,_;
string s;
char p[N];
ll ne[N];
void solve()
{
cin>>s>>p+1;n=s.size(),m=strlen(p+1);
for(int i=2,j=0;i<=m;i++)
{
while(j&&p[i]!=p[j+1])j=ne[j];
if(p[i]==p[j+1])j++;
ne[i]=j;
}
for(int i=0,j=0;i<n;i++)
{
while(j&&s[i]!=p[j+1])j=ne[j];
if(s[i]==p[j+1])j++;
if(j==m)
{
i-=m-1;
s.erase(i,m);
j=ne[j];
i-=m+1;//需要多减一位,因为有i++
}
if(i<=-1)i=-1;//否则会re好多
}
cout<<s;
}
int main()
{
solve();
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define in insert
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+10,M=1e9+7,P=13331;
ll n,m,_;
char s[N],p[N];
ull pi[N],h[N];
ull Hash=0;//模板穿的Hash
char ans[N];//栈模拟
int top;//栈顶指针
ull get(int l,int r)
{
// for(int i=l;i<=r;i++) //一个大坑,因为l可能是个负值
// cout<<s[i]<<" ";//直接运行和debug运行结果不同,
// puts("");//这告诉我们,不要输出一个负下标的值
return h[r]-h[l-1]*pi[r-l+1];
}
void solve()
{
scanf("%s%s",s+1,p+1);
n=strlen(s+1),m=strlen(p+1);
pi[0]=1;
for(int i=1;i<=n;i++)
{
pi[i]=pi[i-1]*P;
// h[i]=h[i-1]*P+s[i];
}
for(int i=1;i<=m;i++)// 模式串的Hash值
{
Hash=Hash*P+p[i];
}
// debug(Hash);
// debug(get(3,5));
// debug(Hash);
for(int i=1;i<=n;i++)
{
ans[++top]=s[i];//栈顶元素入栈
h[top]=h[top-1]*P+s[i];
// debug(h[top]);
// debug(i);
// debug(top);
// debug(i);
// if(top>=m&&Hash==get(top-m+1,top))//get有点问题
//肯定不是get原数组,而是动态的那个栈里边的数组
// if(Hash==h[top]-h[top-m]*pi[m])//两种等价的方法
// {
// top-=m;
// }
if(Hash==get(top-m+1,top))//get有点问题
{ //get想要知道ans数组的Hash
top-=m;
}
}
// cout<<n<<endl<<top<<endl;
for(int i=1;i<=top;i++)
cout<<ans[i];
}
int main()
{
solve();
return 0;
}