• hdu 2203 亲和串


    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
    用kmp算法,模板题
    #include<stdio.h> #include<string.h> int next[200005]; char s1[200005],s2[100005]; int len1,len2; void nextt() {     int i,j;     i=0;     j=-1;     memset(next,-1,sizeof(next));     while(i<len2)     {         if(j==-1 || s2[i]==s2[j])         {             i++;             j++;             next[i]=j;         }         else j=next[j];     } } int kmp() {     int i,j;     i=0;     j=0;     while(i<len1 && j<len2)     {         if(j==-1 || s1[i]==s2[j])         {             i++;             j++;         }         else j=next[j];     }     if(j>=len2)     return 1;     else return 0; } int main() {     int i,flag;     while(scanf("%s",s1)!=EOF)     {         scanf("%s",s2);         len1=strlen(s1);         len2=strlen(s2);         if(len2>len1)         {             printf("no ");             continue;         }         for(i=0;i<len1;i++)         s1[i+len1]=s1[i];         len1=2*len1;         nextt();         flag=kmp();         if(flag)         printf("yes ");         else printf("no ");     }     return 0; }
  • 相关阅读:
    Centos-获取远程主机对应端口信息-telnet
    Centos-跟踪数据传输路由状态-traceroute
    Centos-本机网络连接、运行端口和路由表等信息-netstat
    Centos-远程拷贝-scp
    Centos-配置网络或显示当前网络接口状态-ifconfig
    Centos-挂载和卸载分区-mount
    Centos-退出抽取设备-eject
    Centos-强制将内存中数据写入磁盘-sync
    Centos-检查文件系统并尝试修复-fsck
    数据结构-静态查找表
  • 原文地址:https://www.cnblogs.com/herumw/p/9464858.html
Copyright © 2020-2023  润新知