zoukankan      html  css  js  c++  java
  • hdu 2203亲和串 (kmp)

    #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]!='')
        {
            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;
    }

  • 相关阅读:
    24-反转链表
    23-链表中环的入口节点
    22-链表中倒数第k个节点
    21-调整数组顺序使奇数位于偶数前面
    18-删除链表的节点
    17-打印从1到最大的n位数
    16-数值的整数次方
    15-二进制中1的个数
    14-剪绳子
    13-机器人的运动范围
  • 原文地址:https://www.cnblogs.com/woshijishu3/p/4100366.html
Copyright © 2011-2022 走看看