zoukankan      html  css  js  c++  java
  • 循环移位(Cycle)

    循环移位(Cycle)


    Description

    Cycle shifting refers to following operation on the sting. Moving first letter to the end and keeping rest part of the string. For example, apply cycle shifting on ABCD will generate BCDA. Given any two strings, to judge if arbitrary times of cycle shifting on one string can generate the other one.

    Input

    There m lines in the input, while each one consists of two strings separated by space. Each string only contains uppercase letter 'A'~'Z'.

    Output

    For each line in input, output YES in case one string can be transformed into the other by cycle shifting, otherwise output NO.

    Example

    Input

    AACD CDAA
    ABCDEFG EFGABCD
    ABCD ACBD
    ABCDEFEG ABCDEE
    

    Output

    YES
    YES
    NO
    NO
    

    Restrictions

    0 <= m <= 5000

    1 <= |S1|, |S2| <= 10^5

    Time: 2 sec

    Memory: 256 MB

    1. 原理与要点:读入两个字符串A和B,如果两字符串长度不同,则直接输出No。如果长度相同,则把A串接在A串后面形成一个新串,然后查询B是否是新串的子串,如果是则Yes,否则No
    2. 遇到的问题:
    3. 时间和空间复杂度:时间复杂度(O(N)),空间复杂度(O(N))
    #include "cstdio"
    #include "cstring"
    
    using namespace std;
    const int maxn = 2e5 + 100;
    
    char s[maxn], t[maxn];
    const int SZ = 1<<20;  //快速io
    struct fastio{
        char inbuf[SZ];
        char outbuf[SZ];
        fastio(){
            setvbuf(stdin,inbuf,_IOFBF,SZ);
            setvbuf(stdout,outbuf,_IOFBF,SZ);
        }
    }io;
    int main() {
        while (scanf("%s %s", s, t) != EOF) {
            if (strlen(s) != strlen(t)) {
                printf("NO
    ");
                continue;
            }
            int len = strlen(s);
            for (int i = len; i < len * 2; i++) {
                s[i] = s[i - len];
            }
            s[len * 2] = '';
            if (strstr(s, t) != NULL)
                printf("YES
    ");
            else
                printf("NO
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    【转】 VC MFC 钩子 实现 自绘 窗体 标题栏 非客户区
    Scintilla开源库使用指南(二)
    Scintilla开源库使用指南(一)
    【转】MFC 多文档
    多视图识别
    获得MFC窗口其它类指针的方法
    sql2005 查看数据库或表大小的系统存储过程 sp_spaceused
    哪里是乐土?关于团队良性循环 (转)
    项目管理反思——前言
    项目经理思考——团队
  • 原文地址:https://www.cnblogs.com/albert-biu/p/11542121.html
Copyright © 2011-2022 走看看