KMP修炼-----HDU

本文通过五个具体的HDU在线评测题目介绍了KMP算法的应用场景,包括字符串匹配、循环字符串构造等,提供了完整的代码实现。

1.HDU1711 Number Sequence

题目:
我是超链接
题意:
求小串在大串中第一次的匹配位置。
代码:

#include <cstdio>
using namespace std;
int t[1000005],a[10005],b[1000005],l2,l1;
void sp()
{
    int i,j;
    t[0]=-1;
    for (i=0;i<l2;i++)
    {
        j=t[i];
        while (j!=-1 && b[i]!=b[j]) j=t[j];
        t[i+1]=++j;
    }
}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&l1,&l2);
        for (i=0;i<l1;i++)
          scanf("%d",&a[i]);
        for (i=0;i<l2;i++)
          scanf("%d",&b[i]);
        sp();
        int ans=-1;
        for (i=0;i<l1;i++)
        {
            while (j!=-1 && a[i]!=b[j]) j=t[j];
            j++;
            if (j==l2)
            {
                ans=i-l2+2;
                break;
            }
        }
        printf("%d\n",ans);
    }
}

2.HDU3336 Count the string

题目:
我是超链接
题意:
求字符串的每个前缀在字符串中出现的次数之和。
题解:
这里写图片描述
这里可以让字符串往后撤一位(虚假),0—n-1变成1—n,这样直接蹦到j=-1就可以了
代码:

#include <cstdio>
#include <cstring>
#define Mod 10007
using namespace std;
int t[200005],n;
char st[200005];
void sp()
{
    int i,j;
    t[0]=-1;
    for (i=0;i<n;i++)
    {
        j=t[i];
        while (j!=-1 && st[i]!=st[j]) j=t[j];
        t[i+1]=++j;
    }
}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while (T--)
    {
        int ans=0;
        scanf("%d\n",&n);
        gets(st);
        memset(t,0,sizeof(t));
        sp();
        for (i=1;i<=n;i++)
        {
            j=t[i];
            while (j!=-1) 
              j=t[j],ans=(ans+1)%Mod;       
        }       
        printf("%d\n",ans);
    }
}

3.HDU3746 Cyclic Nacklace

题目:
我是超链接
题意:
求使给出字符串至少循环两次的最少的添加字符的个数。
题解:
一开始理解错题意了,以为还可以在中间添加字母。。。
没想到就是只能在后面加,最小循环节=长度-末位失配
代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int t[100005],l;
char st[100005];
void sp()
{
    int i,j;
    memset(t,0,sizeof(t));
    t[0]=-1;
    for (i=0;i<l;i++)
      {
        j=t[i];
        while (j!=-1 && st[i]!=st[j]) j=t[j];
        t[i+1]=++j;
      }
}
int main()
{
    int T,i;
    scanf("%d\n",&T);
    while (T--)
    {
        gets(st);
        l=strlen(st);
        sp();
        int xh=l-t[l]; 
        if (l%xh==0 && l!=xh) printf("0\n");
        else printf("%d\n",xh-(l%xh));
    }
}

4.HDU 2087 剪花布条

题目:
我是超链接
题意:
求小串在大串中最多不重复匹配次数。
题解:
KMP模板,就是在查询的时候如果找到了就把小字符串跳到0(从头开始)
代码:

#include <cstdio>
#include <cstring>
using namespace std;
int t[1005],l1,l2;
char st[1005],s[1005];
void sp()
{
    int i,j;
    t[0]=-1;
    l2=strlen(st);
    for (i=0;i<l2;i++)
    {
        j=t[i];
        while (j!=-1 && st[i]!=st[j]) j=t[j];
        t[i+1]=++j;
    }
}
int main()
{
    int i;
    while (scanf("%s",s) && s[0]!='#')
    {
        scanf("%s",st);
        l1=strlen(s);
        memset(t,0,sizeof(t));
        sp();   
        int ans=0,j=0;
        for (i=0;i<l1;i++)
        {
            while (j!=-1 && s[i]!=st[j]) j=t[j];
            j++;
            if (j==l2)
            {
                j=0; ans++;
            }
        }
        printf("%d\n",ans);
    }
}

5.HDU 2594 Simpsons’Hidden Talents

题目:
我是超链接
题意:
求最长公共前缀后缀。
题解:
两个方法:1、在中间添加一个字符 2、找出失配之后,比较失配和l1,l2的大小,保证不超出范围
代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char st1[200010],st2[50005];
int t[200010],l,l1,l2,ans;
void sp()
{
    int i,j;
    memset(t,0,sizeof(t));
    t[0]=-1;
    for (i=0;i<l;i++)
    {
        j=t[i];
        while (j!=-1 && st1[i]!=st1[j]) j=t[j];
        t[i+1]=++j;
    }
}
int main()
{
    while (~scanf("%s %s",st1,st2))
    {
        l1=strlen(st1);
        l2=strlen(st2);
        st1[l1]='#';st1[l1+1]='\0';
        strcat(st1,st2);
        l=l1+l2+1;
    //  l=l1+l2;
        sp();
    /*  if (t[l]>min(l1,l2)) ans=min(l1,l2);
        else ans=t[l];*/
        if (t[l]!=0)
        {
            for (int i=0;i<t[l];i++)
              printf("%c",st1[i]);
            printf(" %d\n",t[l]);
        }
        else printf("0\n");
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值