【Tsinghua OJ】循环移位(Cycle)

Posted on 2015-03-21 20:05  Maples7  阅读(2031)  评论(2编辑  收藏  举报

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

描述

所谓循环移位是指。一个字符串的首字母移到末尾, 其他字符的次序保持不变。比如ABCD经过一次循环移位后变成BCDA

给定两个字符串,判断它们是不是可以通过若干次循环移位得到彼此

输入

由m行组成,每行包含两个由大写字母'A'~'Z'组成的字符串,中间由空格隔开

输出

对于每行输入,输出这两个字符串是否可以通过循环移位得到彼此:YES表示是,NO表示否

样例

见英文题面

限制

0 ≤ m ≤ 5000

1 ≤ |S1|, |S2| ≤ 10^5

时间:2 sec

内存:256 MB

 

 

solution:

此题的详细解答请参考清华大学邓俊辉老师《数据结构(C++语言版)》(第三版)Page 327——Karp-Rabin算法。

将问题转化为模式串匹配问题,此算法是基于 哈希表 的(用哈希表比对两串是否匹配)。

 

AC代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 #define M 997
 6 #define R 26
 7 #define MMAX 100005
 8 #define DIGIT(S, i) ( (S)[i] - 'A' )
 9 
10 // IO外挂
11 const int SZ = 1<<20;
12 struct fastio{
13     char inbuf[SZ];
14     char outbuf[SZ];
15     fastio(){
16         setvbuf(stdin,inbuf,_IOFBF,SZ);
17         setvbuf(stdout,outbuf,_IOFBF,SZ);
18     }
19 }io;
20 
21 typedef long long HashCode;
22 
23 bool check1by1(char* P, char* T, size_t i)
24 {
25     for (size_t m = strlen(P), j = 0; j < m; j++, i++)
26     {
27         if (P[j] != T[i]) return false;
28     }
29     return true;
30 }
31 
32 HashCode prepareDm(size_t m)
33 {
34     HashCode Dm = 1;
35     for (size_t i = 1; i < m; i++) 
36         Dm = (Dm*R) % M;
37     return Dm;
38 }
39 
40 void updateHash(HashCode& hashT, char* T, size_t m, size_t k, HashCode Dm)
41 {
42     hashT = (hashT - DIGIT(T, k - 1)*Dm) % M;
43     hashT = (hashT*R + DIGIT(T, k + m - 1)) % M;
44     if (hashT < 0) hashT += M;
45 }
46 
47 bool match(char* P, char* T)
48 {
49     size_t m = strlen(P), n = strlen(T);
50     HashCode Dm, hashP = 0, hashT = 0;
51     Dm = prepareDm(m);
52     for (size_t i = 0; i < m; i++)
53     {
54         hashP = (hashP*R + DIGIT(P, i)) % M;
55         hashT = (hashT*R + DIGIT(T, i)) % M;
56     }
57     for (size_t k = 0;;)
58     {
59         if (hashT == hashP && check1by1(P, T, k)) return true;
60         if (++k>n - m) return false;
61         else updateHash(hashT, T, m, k, Dm);
62     }
63 }
64 
65 int main()
66 {
67     char* s1 = (char*)malloc(sizeof(char)*(MMAX));
68     char* s2 = (char*)malloc(sizeof(char)*(MMAX * 2));
69 
70     do
71     {
72         if (scanf("%s %s", s1, s2) == EOF) break;
73         int n1, n2;
74         n1 = strlen(s1); n2 = strlen(s2);
75         if (n1 != n2) printf("NO\n");
76         else
77         {
78             int i;
79             for (i = n2; i < n2 * 2 - 1; i++) s2[i] = s2[i - n2];
80             s2[i] = '\0';
81 
82             if (match(s1, s2)) printf("YES\n");
83             else printf("NO\n");
84         }
85 
86     } while (true);
87 
88     return 0;
89 }