HDU 2203 亲和串(水·KMP)

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

解题思路:

题目意思应该可以理解为主串复制一遍加到后面,从这个主串找子串。

这题不用KMP都行,调用strstr函数就行了,证明这个函数复杂度哦哦的!

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define debug(x) printf("OK to here %s\n",#x)
using namespace std;

const int N = 1e5+5;

char s[N*2],t[N],ss[N];
int fail[N];

void kmp()
{
    int len = strlen(s);
    int llen = strlen(ss);
    fail[0] = -1;
    for (int i=0,j=-1;i<llen;){
        if (j==-1||ss[i]==ss[j]){
            i++;j++;
            fail[i] = j;
        }
        else j = fail[j];
    }

    for (int i=0,j=0;i<len;){
        if (j==-1||s[i]==ss[j]){
            i++;j++;
        }
        else j = fail[j];

        if (j==llen){
            printf("yes\n");
            return ;
        }
    }
    printf("no\n");
}

int main()
{
    while (~scanf("%s %s",t,ss)){
        strcpy(s,t);//debug(x);
        strcat(s,t);
        //kmp();
        if (strstr(s,ss)) puts("yes");
        else puts("no");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43768644/article/details/94356172