“21天好习惯”第一期-6 kmp

21天零基础入门ACM

21天零基础入门ACM之 第6天

kmp

KMP

kmp应该算是字符串匹配的基本算法了。
什么是kmp呢?

KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特—莫里斯—普拉特操作(简称KMP算法)。KMP算法的核心是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的。具体实现就是通过一个next()函数实现,函数本身包含了模式串的局部匹配信息。KMP算法的时间复杂度O(m+n)
----百度百科

其实kmp就是用来快速解决字符串匹配问题的。
kmp之所以能快速的原因是他预处理了字符串的最大公共前后缀,从而能在失配的时候,减少不必要的回溯。

算法模板:
int n;
string a,b;
int next[N];

void getnext(string a){
	next[0]=-1;
	int i=0,j=-1;
	while(i<a.size()){
		if(j==-1||a[i]==a[j]){
			i++,j++,next[i]=j;
		}
		else j=next[j];
	}
}

void kmp(string a,string b){
	int i=0,j=0;
	while(i<a.size()){
		if(j==-1||a[i]==b[j]){
			i++,j++;
		}
		else j=next[j];
		if(j==b.size()) cout<<i-j+1<<endl;
	}
}

例题:

题目1:

https://www.luogu.com.cn/problem/P3375
在这里插入图片描述

代码:
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",x)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    
ll qpow(ll a, ll b, ll mod) { ll ans = 1;  a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();    return s * w; }

const int mod=1e9+7;
const int N=1e6+7;

int n;
string a,b;
int next[N];

void getnext(string a){
	next[0]=-1;
	int i=0,j=-1;
	while(i<a.size()){
		if(j==-1||a[i]==a[j]){
			i++,j++,next[i]=j;
		}
		else j=next[j];
	}
}

void kmp(string a,string b){
	int i=0,j=0;
	while(i<a.size()){
		if(j==-1||a[i]==b[j]){
			i++,j++;
		}
		else j=next[j];
		if(j==b.size()) cout<<i-j+1<<endl;
	}
}

int main(){
	while(cin>>a>>b){
		getnext(b);
		kmp(a,b);
		for(int i=1;i<=b.size();i++) cout<<next[i]<<" ";
		cout<<endl;
	}
	return 0;
}
题目2:

https://www.luogu.com.cn/problem/P4391
在这里插入图片描述
代码:

#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",x)
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    
ll qpow(ll a, ll b, ll mod) { ll ans = 1;  a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();    return s * w; }

const int mod=1e9+7;
const int N=1e6+7;

int n;
string a,b;
int next[N];

void getnext(string a){
	int j=-1,i=0;
	next[0]=-1;
	while(i<a.size()){
		if(j==-1||a[i]==a[j]){
			i++,j++,next[i]=j;
		}
		else j=next[j];
	}
}

int main(){
    while(cin>>n>>a){
    	getnext(a);
    	cout<<n-next[n]<<endl;
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述

代码如下:
#include <bits/stdc++.h>
using namespace std;
#define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define sc(x) scanf("%d",x)
#define next ha
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    
ll qpow(ll a, ll b, ll mod) { ll ans = 1;  a%=mod; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }    while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();    return s * w; }

const int mod=1e9+7;
const int N=1e6+7;

int n;
string a,b;
int next[N];

void getnext(string a){
    next[0]=-1;
    int i=0,j=-1;
    while(i<a.size()){
        if(j==-1||a[i]==a[j]){
            i++;j++;next[i]=j;
        }
        else j=next[j];
    }
}


int res[N];
int main() {
    ios::sync_with_stdio(0);
    string s;cin >> s;
    getnext(s);
    for (int i = 1;i < s.size();++i)
        ++res[next[i]];
    int ans = next[s.size()];
    if (ans <= 0)
        cout << "Just a legend\n";
    else if (res[ans])
        cout << s.substr(0, ans) << endl;
    else if (next[ans] > 0)
        cout << s.substr(0, next[ans]) << endl;
    else
        cout << "Just a legend\n";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值