• hdu2203 KMP水的问题


    两种方法     首先是纯KMP

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;


    char str1[200010],str2[100010];
    int next[100010];
    int get()
    {
    next[0]=-1;
    int j=0;
    int k=-1;
    int len=strlen(str2);
    while(j<len-1)
    {
    if(k==-1||str2[k]==str2[j])
    {
    j++;
    k++;
    next[j]=k;
    }
    else k=next[k];
    }
    return 0;
    }
    int main()
    {
    int i,j;
    while(~scanf("%s%s",str1,str2))
    {
    get();
    char str[100010];
    strcpy(str,str1);
    strcat(str1,str);
    //printf("&&&& ");
    int len1=strlen(str1);
    int len2=strlen(str2);
    int i=j=0;
    while(i<len1&&j<len2)
    {
    if(j==-1||str1[i]==str2[j])
    {
    i++;
    j++;
    }
    else j=next[j];
    }
    if(j==len2) printf("yes ");
    else printf("no ");
    }
    return 0;
    }



    以下是用了STL里面的函数

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;


    int main()
    {
    char str1[200010],str2[100010],str[100010];
    int i,j;
    while(~scanf("%s%s",str1,str2))
    {
    strcpy(str,str1);
    strcat(str1,str);
    if(strstr(str1,str2)) printf("yes ");
    else printf("no ");

    return 0;
    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    常用排序算法
    多线程基础知识 转
    转 大型项目架构演进过程
    TCP/IP 思维导图
    Java8 List字符串 去重
    docker lnmp php
    jpa 批量插入
    备忘提醒
    IntelliJ IDEA像Eclipse一样打开多个项目(转)
    IntelliJ Idea 常用快捷键列表
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4676212.html
Copyright © 2020-2023  润新知