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

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=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
     
    思路:他说要循环移位,那a串后再接一个a串后就是裸的kmp了。
    #include <bits/stdc++.h>
    #define PB push_back
    #define MP make_pair
    using namespace std;
    typedef long long LL;
    typedef pair<int,int> PII;
    #define PI acos((double)-1)
    #define E exp(double(1))
    #define K 1000000+9
    int nt[K];
    string sa,sb;
    void kmp_next(void)
    {
        nt[0]=0;
        for(int i=1,j=0,len=sb.length();i<len;i++)
        {
            while(j&&sb[j]!=sb[i])j=nt[j-1];
            if(sb[j]==sb[i])j++;
            nt[i]=j;
        }
    }
    int kmp(void)
    {
        kmp_next();
        for(int i=0,j=0,lena=sa.length(),lenb=sb.length();i<lena;i++)
        {
            while(j&&sa[i]!=sb[j])j=nt[j-1];
            if(sa[i]==sb[j])j++;
            if(j==lenb)
                return 1;
        }
        return 0;
    }
    int main(void)
    {
        while(cin>>sa>>sb)
        {
            sa+=sa;
            if(kmp())
                printf("yes
    ");
            else
                printf("no
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
    ros topic 发布一次可能会接收不到数据
    python中的print()、str()和repr()的区别
    python 部分函数
    uiautomatorviewer错误 unable toconnect to adb
    pyqt 不规则形状窗口显示
    appium 计算器demo
    Spring 3.0 注解注入详解
    Spring Autowire自动装配
    restful 学习地址
  • 原文地址:https://www.cnblogs.com/weeping/p/5734983.html
Copyright © 2011-2022 走看看