循環移位(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\n");
            continue;
        }
        int len = strlen(s);
        for (int i = len; i < len * 2; i++) {
            s[i] = s[i - len];
        }
        s[len * 2] = '\0';
        if (strstr(s, t) != NULL)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM