码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2203亲和串 (kmp)

时间:2014-11-15 23:06:24      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   sp   on   bs   amp   nbsp   c   

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int Next[100005];
void getnext(const char *W,int *next)
{
    int j=0,k=-1;
    next[0]=-1;
    while(!j || W[j]!=‘\0‘)
    {
        if(k==-1 || W[j]==W[k])
        {
            j++;
            k++;
            if(W[j]!=W[k])
                next[j]=k;
            else
                next[j]=next[k];
        }
        else k=next[k];
    }
}
int KMP(const char *T,const char *W)
{
    int i=0,j=0,num=0;
    getnext(W,Next);
    int tlen,wlen;
    tlen=strlen(T);
    wlen=strlen(W);
    while(i<tlen)
    {
        if(j==-1 || T[i]==W[j])
        {
            i++;
            j++;
        }
        else
        {
            j=Next[j];
        }
        if(j==wlen)
        {
            return 1;
        }
    }
    return 0;
}
char str[200005],str1[100005],str2[100005];
int main()
{
    int n;
    while(cin>>str1>>str2)
    {
        int len=strlen(str1);
        int len1=strlen(str2);
        if(len1>len) {cout<<"no"<<endl;
        continue;
        }
        strcpy(str,str1);
        strcpy(&str[len],str1);
        if(KMP(str,str2)) cout<<"yes"<<endl;
        else  cout<<"no"<<endl;
    }
    return 0;
}

hdu 2203亲和串 (kmp)

标签:io   ar   os   sp   on   bs   amp   nbsp   c   

原文地址:http://www.cnblogs.com/woshijishu3/p/4100366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!