Given a string…
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 1819   Accepted: 390
Case Time Limit: 2000MS

Description

Peter’s Boss is now very upset. He said that Peter’s vision of the orthogonal sum of two strings is not collinear to the general pary line of RIGS. At least, it is very bad that the orthogonal sum of two strings in Peter’s vision can be different depending on a selected set of strings. But Boss decided to give Peter a last str…well, a chance.

Peter’s colleague Andrew invented another definition of orthogonal sum of two strings of equal length n, which depends only on the alphabet. The basic alphabet to define this operation consists only of zeros and ones. The orthogonal sum of two strings a ⊕ b is just a string c where ci = ai ⊕ bi (Si denotes i-th character of string S). Here ⊕ stands for exclusive OR operation which returns 0 for equal characters and 1 otherwise.

Now Peter must study properties of orthogonal closure of a given string S. The orthogonal closure of S (denoted S) is a set of strings S(k) ⊕ S(l) for any 0 ≤ kl ≤ n − 1, where n is the length of S, and S(k) denotes an operation of k-th circular shift of S — moving k last characters from the end of the string S to its beginning. For example, the second circular shift of abcde is deabc.

Given a string T, Peter’s task is to check whether it belongs to S. Could you solve this task for him?

Input

The first line of the input file contains a given string T. The second line contains S. Both strings are of equal length in range from 1 to 5 000 characters. All characters in these strings are zeros or ones.

Output

If a given string belongs to S, output “Yes”. Otherwise output “No”.

Sample Input

#1 11111
10101
#2 11110
10101

Sample Output

#1 No
#2 Yes

Source

Northeastern Europe 2007, Northern Subregion
 
  题意:给你两个串只有0和1的T,S,其中S可以进行循环右移,问你S里面是否存在两个长度都为|S|的子串它们的异或结果等于T。
  算是比较裸的KMP,但是比赛的时候WA了,时候才发现求next数组的对象错了,所以这里记录一下,避免以后再犯这种错误。
  我们平时使用KMP一般只用到next数组,但是对于真的使用KMP求字符串匹配的话,需要求next数组的对象是模式串而不是主串。AC自动机就是把一堆模式串构造出AC自动机。
 
上代码:
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 100002
#define ll long long
#define XOR(x,y) ( x==y ? '0' : '1')
using namespace std; char s[MAX],t[MAX],e[MAX];
int next[MAX]; void get_next(char *p,int ls){
int k,i;
k=-;i=;
memset(next,-,sizeof(next));
while(i<=ls-){
if(k==- || p[i]==p[k]){ k++; i++; next[i]=k;}
else k=next[k];
}
} int kmp(int st,int ls,int lt){
int i,j;
i=;j=;
for(int k=;k<lt;k++) e[k]=XOR(s[st+k],t[k]);
e[lt]='\0';
get_next(e,lt);
while(i<ls && j<lt){
if(j==- || s[i]==e[j]){ i++; j++;}
else j=next[j];
}
if(j>=lt) return (i-lt+);
return -;
} int main(){
int f,lt,ls;
//freopen("data.txt","r",stdin);
while(scanf("%s",t)!=EOF){
scanf("%s",s);
lt=strlen(t);
ls=strlen(s);
for(int i=;i<ls;i++) s[i+ls]=s[i];
s[ls*]='\0';
f=-;
for(int i=;i<ls;i++){
f=kmp(i,ls*,lt);
if(f!=-) break;
}
if(f!=-) printf("Yes\n");
else printf("No\n");
}
return ;
}

/*3541*/

POJ - 3541 - Given a string…的更多相关文章

  1. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  2. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  3. poj 3729 Facer’s string

    Facer’s string Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2155   Accepted: 644 Des ...

  4. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

  5. POJ 3376 Finding Palindromes EX-KMP+字典树

    题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...

  6. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  7. next_permutation函数

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类 ...

  8. buaaoj230——next_permutation的应用

    题目地址 简单的全排列输出,借用stl中的next_permutation就非常简单了. 关于next_permutation:(备忘,来源网络) /*这是一个求一个排序的下一个排列的函数,可以遍历全 ...

  9. 8-全排列next_permutation

    C++中全排列函数next_permutation 用法 转载 2017年03月29日 14:38:25 1560 全排列参考了两位的博客 感谢! http://blog.sina.com.cn/s/ ...

随机推荐

  1. Android SDK Manager 无法更新问题(转载)

    先看看如何加快更新速度,再说如何更新. 首先更新host文件,如图,打开目录 C:\Windows\System32\drivers\etc,在目录下有hosts文件 打开方式选用“记事本”打开 将一 ...

  2. Python机器学习算法 — KNN分类

    KNN简介 K最近邻(k-Nearest Neighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一.KNN分类算法属于监督学习. 最简单最初级的分类器是将全部的训练 ...

  3. 【洛谷4219】[BJOI2014]大融合(线段树分治)

    题目: 洛谷4219 分析: 很明显,查询的是删掉某条边后两端点所在连通块大小的乘积. 有加边和删边,想到LCT.但是我不会用LCT查连通块大小啊.果断弃了 有加边和删边,还跟连通性有关,于是开始yy ...

  4. Linux命令(004) -- watch

    对Linux系统的操作过程中,经常会遇到重复执行同一命令,以观察其结果变化的情况.惯用的方法是:上下键加回车,或是Ctr+p然后回车.今天我们来了解一下watch命令,它可以帮助我们周期性的执行一个命 ...

  5. [hihocoder][Offer收割]编程练习赛58

    最大的K-偏差排列 每次取可选范围里的最大的数字,如果最左侧的数字还没有使用就直接使用最左侧的数字 #pragma comment(linker, "/STACK:102400000,102 ...

  6. SSH Secure Shell Client连接centos6.5时中文字乱码处理

    在学习Linux的过程中,最先碰到的是通过SSH终端连接时发现有乱码出现,使用这篇文章先从这里说起. 在 ssh , telnet 终端中文显示乱码解决办法#vim /etc/sysconfig/i1 ...

  7. node的api

    一. 1.url: 绝对URI http://user:pass@www.example.com:80/dir/index.html?uid=1#ch1 协议 登录信息 服务器地址 端口 文件路径 查 ...

  8. window.showModalDialog的问题

    通过window.showModalDialog的方式弹出B页面,总报“拒绝访问”的错误,将站点添加到受信任站点可以解决这个问题

  9. Python3之切片的道理

    list的切片有三个参数:起点,终点,步长 list[::-1] 相当于起点为最后的一个,终点为第一个,然后一次减少一个 更多的看下面的测试 >>> a = [0,1,2,3,4,5 ...

  10. html5——3D案例(立体导航)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...