KMP模版题--》水水更自信

KMP

写这篇博客时自己对KMP也没有理解透彻,只是找了些模版题做做,发现NEXT数组用途真的很广泛,也许慢慢做着做着就理解了。

hdu 1711 Number Sequence


题意:给你两个数组,让你从数组1中找到从序号K开始一次能与数组2匹配的K的最小值

代码:
/*
#include<iostream>
#include<iostream>
#include<cstdio>
using namespace std;

#define M 10005
#define MM 1000005
int a[MM],b[M],next[M];
int n,m;

void get_next(){
    next[0]=-1,next[1]=0;
    int i=1,j=0;
    while(i<n){
        if(j==-1||b[i]==b[j]){
            i++;j++;
            next[i]=j;
        }
        else j=next[j];
    }
}
int KMP(){
    int i=0,j=0;
    while(j!=n&&i<m){
        if(j==-1||a[i]==b[j]){
            i++;j++;
        }
        else j=next[j];
    }
    if(j==n) return i-n+1;
    else return -1;
}

int main(){
    int T;
    cin>>T;
    while(T--){
        cin>>m>>n;
        for(int i=0;i<m;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);
        get_next();
        printf("%d\n",KMP());
    }
    return 0;
}
*/
#include<iostream>
#include<cstdio>
using namespace std;

#define MAX1 1000005
#define MAX2 10005
int a[MAX1],b[MAX2],next[MAX2];
int m,n;

void GetNext(){
   next[0]=-1,next[1]=0;
   int i=1,j=0;
   while(i<n){
        if(j==-1||b[i]==b[j]){
            i++,j++;
            next[i]=j;
        }
        else j=next[j];
   }
}
int KMP(){
    int i=0,j=0;
    while(i<m&&j<n){
        if(j==-1||a[i]==b[j])
            i++,j++;
        else
            j=next[j];
    }
    if(j==n) return i-j+1;
    else return -1;
}

int main(){
    int T;
    cin>>T;
    while(T--){
        cin>>m>>n;
        for(int i=0;i<m;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);
        GetNext();
        printf("%d\n",KMP());
    }
    return 0;
}


hdu 1686 Oulipo

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686

题目大意:给你两个字符串,让你找到字符串1中有几个与字符2匹配的子串

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#define M 10010
#define MM 1000010
using namespace std;

char W[M],T[MM];
int next[M],tot,dw,dt;

void get_next(){
    next[0]=-1;
    next[1]=0;
    int i=1,j=0;
    while(i<dw){
        if(j==-1||W[i]==W[j]){
            i++,j++;
            next[i]=j;
        }
        else j=next[j];
    }
}
void KMP(){
    int i=0,j=0;
    while(i<dt&&j<dw){
        if(j==-1||T[i]==W[j]){
            i++,j++;
            if(j==dw) tot++,j=next[j];
        }
        else{
            j=next[j];
        }
    }
}
int main(){
    int ca;
    cin>>ca;
    while(ca--){
        cin>>W;
        cin>>T;
        dw=strlen(W);
        dt=strlen(T);
        tot=0;
        get_next();
        KMP();
        cout<<tot<<endl;
    }
    return 0;
}



hdu 2594 Simpsons’ Hidden Talents


题目大意:给你两个字符串,求字符串1的前缀与字符串2的后缀能匹配的最大子串

题目代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAX 50010
char s1[2*MAX],s2[MAX];
int d,d1,d2;
int next[2*MAX];
void get_next(){
    strcat(s1,s2);
    d=d1+d2;
    next[0]=-1,next[1]=0;
    int i=1,j=0;
    while(i<d){
        if(j==-1||s1[i]==s1[j]){
            i++,j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}
int main(){
    while(cin>>s1>>s2){
        d1=strlen(s1);
        d2=strlen(s2);
        get_next();
        while(next[d]>d1||next[d]>d2)
            d=next[d];

        for(int i=0;i<next[d];i++){
            cout<<s1[i];
        }
        if(next[d]!=0) cout<<" "<<next[d]<<endl;
        else cout<<0<<endl;
    }
    return 0;
}



hdu 3336 Count the string


题目大意:给你一个字符串,问你该字符串所有的前缀串在原字符中出现的次数

题目代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 200010
#define MOD 10007
using namespace std;
char s[N];
int next[N],dp[N];
int n;
void get_next(){
    next[0]=-1,next[1]=0;
    int i=1,j=0;
    while(i<n){
        if(j==-1||s[i]==s[j]){
            i++,j++;
            next[i]=j;
        }
        else j=next[j];
    }
}
int main(){
    int ca;
    cin>>ca;
    while(ca--){
        cin>>n>>s;
        get_next();
        dp[0]=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            dp[i]=dp[next[i]]+1;
            dp[i]%=MOD;
            ans+=dp[i];
            ans%=MOD;
        }
        cout<<ans<<endl;
    }
    return 0;
}


poj 2752 Seek the Name, Seek the Fame

题目链接:http://poj.org/problem?id=2752


题目大意:给你一个字符串,求出所有满足字符串前k个字符与后K个字符相匹配的K的值。


题目代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 400010
char s[N];
int next[N],a[N];
int d;
void get_next(){
    next[0]=-1,next[1]=0;
    int i=1,j=0;
    while(i<d){
        if(j==-1||s[i]==s[j]){
            i++,j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}
int main(){
    while(cin>>s){
        d=strlen(s);
        get_next();
        int x=0,k=next[d];
        a[0]=d;
        while(k!=0){
            x++;
            a[x]=k;
            k=next[k];
        }
        for(int i=x;i>0;i--)
            cout<<a[i]<<" ";
        cout<<a[0];
        cout<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值