【Tsinghua OJ】循环移位(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

描述

所谓循环移位是指。一个字符串的首字母移到末尾, 其他字符的次序保持不变。比如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 }


【Tsinghua OJ】循环移位(Cycle)

时间: 2024-12-18 21:56:19

【Tsinghua OJ】循环移位(Cycle)的相关文章

【Tsinghua OJ】祖玛(Zuma)问题

描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨 道上并加入原有序列中.一旦有三个或更多同色的珠子变成相邻,它们就会立即消失.这类消除现象可能会连锁式发生,其间你将暂时不能发射珠子. 开发商最近准备为玩家写一个游戏过程的回放工具.他们已经在游戏内完成了过程记录的功能,而回放功能的实现则委托你来完成. 游戏过程的记录中,首先是轨道上初始的珠子序列,然后是玩家接下来所做的一系列操作.你的任务是,在各次操作

【Tsinghua OJ】灯塔(LightHouse)问题

描述 海上有许多灯塔,为过路船只照明.从平面上看,海域范围是[1, 10^8] × [1, 10^8] . (图一) 如图一所示,每个灯塔都配有一盏探照灯,照亮其东北.西南两个对顶的直角区域.探照灯的功率之大,足以覆盖任何距离.灯塔本身是如此之小,可以假定它们不会彼此遮挡. (图二) 若灯塔A.B均在对方的照亮范围内,则称它们能够照亮彼此.比如在图二的实例中,蓝.红灯塔可照亮彼此,蓝.绿灯塔则不是,红.绿灯塔也不是. 现在,对于任何一组给定的灯塔,请计算出其中有多少对灯塔能够照亮彼此. 输入 共

【Tsinghua OJ】隧道(Tunel)问题

描述 现有一条单向单车道隧道,每一辆车从隧道的一端驶入,另一端驶出,不允许超车 该隧道对车辆的高度有一定限制,在任意时刻,管理员希望知道此时隧道中最高车辆的高度是多少 现在请你维护这条隧道的车辆进出记录,并支持查询最高车辆的功能 输入 第一行仅含一个整数,即高度查询和车辆出入操作的总次数n 以下n行,依次这n次操作.各行的格式为以下几种之一: 1. E x //有一辆高度为x的车进入隧道(x为整数) 2. D //有一辆车离开隧道 3. M //查询此时隧道中车辆的最大高度 输出 若D和M操作共

Tsinghua OJ Zuma

Description Let's play the game Zuma! There are a sequence of beads on a track at the right beginning. All the beads are colored but no three adjacent ones are allowed to be with a same color. You can then insert beads one by one into the sequence. O

LeetCode OJ - Linked List Cycle II

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 解题思路: 设置两个指针slow和fast,从head开始,slow一次一步,fast一次两步,如果fast能再次追上slow则有圈. 设slow走了n步,则fast走了2*n步,设圈长度m

LeetCode OJ - Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 解题思路: 使用快慢指针,快指针每次走两步,慢指针每次走一步,若快指针能追上慢指针,则表明有圈. 代码如下: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNo

LeetCode OJ:Linked List Cycle(链表循环)

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 判断一个链表是否存在环,维护快慢指针就可以,如果有环那么快指针一定会追上慢指针,代码如下: 1 class Solution { 2 public: 3 bool hasCycle(ListNode *head) { 4 ListNode * slow, * fast; 5 sl

&lt;LeetCode OJ&gt; 141. Linked List Cycle

141. Linked List Cycle My Submissions Question Total Accepted: 88665 Total Submissions: 241622 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Subscribe to see which co

LeetCode OJ 142. Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Subscribe to see which companies asked this question 解答: 先用快慢指针