• HDU 2203 亲和串 (KMP)


    亲和串

    http://acm.hdu.edu.cn/showproblem.php?pid=2203

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4676    Accepted Submission(s): 2109

    Problem Description
    人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。 亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串。
     
    Input
    本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二行包含输入字符串s2,s1与s2的长度均小于100000。
     
    Output
    如果s2是s1的亲和串,则输出"yes",反之,输出"no"。每组测试的输出占一行。
     
    Sample Input
     
    AABCD
    CDAA
    ASD
    ASDF
     
    Sample Output
     
    yes
    no
     
    Author
    Eddy
     
    Recommend
    lcy
     
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int N=100010;
    
    char str1[N<<1],str2[N];
    int next[N],flag;
    
    void getnext(int len){      //获取字符串str2的next函数,标记
        int i=0,j=-1;
        next[0]=-1;
        while(i<len){
            if(j==-1 || str2[i]==str2[j]){
                i++;j++;
                next[i]=j;
            }else
                j=next[j];
        }
    }
    
    void KMP(int len1,int len2){    //KMP查找
        int i=0,j=0;
        getnext(len2);
        while(i<len1){
            if(j==-1 || str1[i]==str2[j]){
                i++;j++;
            }else
                j=next[j];
            if(j==len2){
                flag=1;
                break;
            }
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%s%s",str1,str2)){
            int len1=strlen(str1);
            int len2=strlen(str2);
            if(len1<len2){
                puts("no");
                continue;
            }
            for(int i=0;i<len1;i++)
                str1[len1+i]=str1[i];
            flag=0;
            KMP(len1<<1,len2);
            if(flag)
                puts("yes");
            else
                puts("no");
        }
        return 0;
    }
     
  • 相关阅读:
    在MyEclipse中运行tomcat报错 严重: Error starting static Resources
    MyEclipse 2015 运行tomcat 内存溢出的解决方法
    (转)Tomcat内存设置详解
    Object调用控件的办法
    Hibernate主键生成方式之hilo
    (转)“中国第一程序员” 求伯君的传奇经历
    雷军相识求伯君
    (转)雷军重掌金山幕后:与求伯君暗战三年两次逼宫
    华军软件发展及盈利模式
    中年人编程
  • 原文地址:https://www.cnblogs.com/jackge/p/2845981.html
Copyright © 2020-2023  润新知