hdu 2203 亲和串(给两个字符串s1,s2,问s2可不可能出现在以s1为循环节的串中)

1.strcat函数,strcat(char *s , char *p);注意这里的s和p所指内存区域不可以重叠s必须有足够的空间来容纳p的字符串

2.strcpy函数,strcpy(char *s,char *p),将p拷贝到s

3.代码:

#include<cstdio>
#include<cstring>
using namespace std;

char s1[1000000],s2[100005],t[1000000];
int len1,len2;
//int LCPS[100005];
int next[100005];

void GetLCPS()
{
    int j=0;
    int k=-1;
    next[0]=-1;
    while(j<len2)
    {
        if(k==-1||s2[k]==s2[j])
        {
            //LCPS[j++]=++k;
            j++;
            ++k;
            next[j]=k;
        }
        else
        {
            //if(k-1>=0)
            k=next[k];
            //else
            //    k=-1;
        }
    }

}

void KMP()
{
    int i=0;
    int j=0;
    while(i<len1&&j<len2)
    {
        if(j==-1||s1[i]==s2[j])
        {
            j++;
            i++;
        }
        else
            j=next[j];
    }
    if(j==len2)
        printf("yes\n");
    else
        printf("no\n");
}

int main()
{
    while(scanf("%s%s",s1,s2)==2)
    {
        len1=strlen(s1);
        len2=strlen(s2);
        while(len1<len2)
        {
            strcpy(t,s1);
            strcat(s1,t);
            len1=strlen(s1);
        }
        strcpy(t,s1);
        strcat(s1,t);
        len1=strlen(s1);
        GetLCPS();
        KMP();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值