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代码如下:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h> #define M 997
#define R 26
#define MMAX 100005
#define DIGIT(S, i) ( (S)[i] - 'A' ) // IO外挂
const int SZ = <<;
struct fastio{
char inbuf[SZ];
char outbuf[SZ];
fastio(){
setvbuf(stdin,inbuf,_IOFBF,SZ);
setvbuf(stdout,outbuf,_IOFBF,SZ);
}
}io; typedef long long HashCode; bool check1by1(char* P, char* T, size_t i)
{
for (size_t m = strlen(P), j = ; j < m; j++, i++)
{
if (P[j] != T[i]) return false;
}
return true;
} HashCode prepareDm(size_t m)
{
HashCode Dm = ;
for (size_t i = ; i < m; i++)
Dm = (Dm*R) % M;
return Dm;
} void updateHash(HashCode& hashT, char* T, size_t m, size_t k, HashCode Dm)
{
hashT = (hashT - DIGIT(T, k - )*Dm) % M;
hashT = (hashT*R + DIGIT(T, k + m - )) % M;
if (hashT < ) hashT += M;
} bool match(char* P, char* T)
{
size_t m = strlen(P), n = strlen(T);
HashCode Dm, hashP = , hashT = ;
Dm = prepareDm(m);
for (size_t i = ; i < m; i++)
{
hashP = (hashP*R + DIGIT(P, i)) % M;
hashT = (hashT*R + DIGIT(T, i)) % M;
}
for (size_t k = ;;)
{
if (hashT == hashP && check1by1(P, T, k)) return true;
if (++k>n - m) return false;
else updateHash(hashT, T, m, k, Dm);
}
} int main()
{
char* s1 = (char*)malloc(sizeof(char)*(MMAX));
char* s2 = (char*)malloc(sizeof(char)*(MMAX * )); do
{
if (scanf("%s %s", s1, s2) == EOF) break;
int n1, n2;
n1 = strlen(s1); n2 = strlen(s2);
if (n1 != n2) printf("NO\n");
else
{
int i;
for (i = n2; i < n2 * - ; i++) s2[i] = s2[i - n2];
s2[i] = '\0'; if (match(s1, s2)) printf("YES\n");
else printf("NO\n");
} } while (true); return ;
}

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

  1. 【Tsinghua OJ】灯塔(LightHouse)问题

    描述 海上有许多灯塔,为过路船只照明.从平面上看,海域范围是[1, 10^8] × [1, 10^8] . (图一) 如图一所示,每个灯塔都配有一盏探照灯,照亮其东北.西南两个对顶的直角区域.探照灯的 ...

  2. 【Tsinghua OJ】祖玛(Zuma)问题

    描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨 道上并加入原有序列中.一旦有三个或更多同色的珠子变成相 ...

  3. 【Tsinghua OJ】范围查询(Range)问题

    [问题描述]数轴上有n个点,对于任一闭区间 [a, b],试计算落在其内的点数. [输入]第一行包括两个整数:点的总数n,查询的次数m.第二行包含n个数,为各个点的坐标.以下m行,各包含两个整数:查询 ...

  4. 【Tsinghua OJ】多米诺骨牌(domino)问题

    (domino.c/cpp)[问题描述] 小牛牛对多米诺骨牌有很大兴趣,然而她的骨牌比较特别,只有黑色和白色的两种.她觉 得如果存在连续三个骨牌是同一种颜色,那么这个骨牌排列便是不美观的.现在她有n个 ...

  5. 【Tsinghua OJ】隧道(Tunel)问题

    描述 现有一条单向单车道隧道,每一辆车从隧道的一端驶入,另一端驶出,不允许超车 该隧道对车辆的高度有一定限制,在任意时刻,管理员希望知道此时隧道中最高车辆的高度是多少 现在请你维护这条隧道的车辆进出记 ...

  6. Tsinghua OJ Zuma

    Description Let's play the game Zuma! There are a sequence of beads on a track at the right beginnin ...

  7. ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))

    TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...

  8. ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))

    这一题是TsingHua OJ上的一道题目,学堂在线的一位数据结构老师的题目(原创),所以我直接把题目先贴下来了,这道题对复习双向链表很有帮助,而且也对数据结构中List,也就是对列表的回顾也是很有帮 ...

  9. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

随机推荐

  1. PHP Warning: ob_start() : output handler 'ob_gzhandler conflicts with 'zlib output compression'

    安装phpcms过程中,会遇到Warning:  ob_start() : output handler 'ob_gzhandler conflicts with 'zlib output compr ...

  2. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

    A simple stone game                                                                                  ...

  4. mvc json post执行顺序

    function GetFlightNo() {        var falg = false; var value = $("#No").val();        $.pos ...

  5. Windows上模拟Linux环境

    有两种方法,一直是 MinGW http://jingyan.baidu.com/article/8cdccae985cf7c315413cd35.html 另外可以用 cygwin http://j ...

  6. 登陆验证前对用户名和密码加密之后传输数据---base64加密

    以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...

  7. UML类图关系大全

    UML类图关系大全 1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法.在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设 ...

  8. iOS --- 通过openURL实现APP之间跳转并传递数据

    在IOS中,实现一个应用启动另外一个应用,使用UIApplication的openURL:方法就可实现,这里以test跳到test02为例.(需要先创建这两个工程) 注册自定义URL协议(在test中 ...

  9. Flume Hello World!

    Flume 是 Cloudera 公司开源出来的一套日志收集系统.模型如下所示: 图中Source,Sink分别代表数据源和数据目的地,channel表示Source和Sink之间的通道.配置文件为/ ...

  10. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...