Cycle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 677    Accepted Submission(s): 245

Problem Description
Alice
get two strings and the lengths are both N. Bored Alice wanna know
whether all equal length prefix string of these two strings are
CycleEqual. For example, "abc" and "bca" and "cab" are CycleEqual. So
you need output N character, '0' is no, '1' is yes.
Input
The input contains multiple test cases.
For each test case, the first contains one string and the second line contains one string. The lengths of strings are both N(1≤N≤10000).
Output
For
each test case, output N characters. For ith character, if prefix
strings of first i character are CycleEqual, output '1', else output
'0'.
Sample Input
abc
cab
aa
aa
Sample Output
001
11
Author
ZSTU
Source
题意:给你两个长度相等的字符串,问他们的所有的前缀能否构成循环同构
假如str1,str2循环同构 则 str1=u+v  str2=v+u;
其实我们只有在kmp的匹配过程中  每匹配一次(S[i]==T[j+1])成功的时候我们判断
可以用字符串hash判断是否相等
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
const LL base=;
LL p[N];
LL Hash[][N];
char a[N];
char b[N];
int Next[N];
int ans[N];
void init(){
p[]=;
for(int i=;i<=;i++)p[i]=p[i-]*base;
}
LL get_val(int l,int r,int t){
LL ans=Hash[t][r]-Hash[t][l-]*p[r-l+];
return ans;
}
int check(int i,int j,int t){
i++;
j++;
if(i==j)return ;
if(get_val(j+,i,t^)==get_val(,i-j,t))return ;
return ;
}
void get_next(char *T){
int len=strlen(T);
Next[]=-;
int j=-;
for(int i=;i<len;i++){
while(j!=-&&T[j+]!=T[i])j=Next[j];
if(T[j+]==T[i])j++;
Next[i]=j;
}
}
void kmp(char *S,char *T,int t){
int lens=strlen(S);
int lent=strlen(T);
get_next(T);
// for(int i=0;i<lent;i++)cout<<Next[i]<<" ";
//cout<<endl;
int j=-;
for(int i=;i<lens;i++){
while(j!=-&&T[j+]!=S[i])j=Next[j];
if(S[i]==T[j+]){
j++;
//cout<<i<<" "<<j<<endl;
if(ans[i]==){
ans[i]=check(i,j,t);
}
}
}
}
int main(){
init();
while(scanf("%s%s",a+,b+)!=EOF){
memset(ans,,sizeof(ans));
//cout<<a+1<<" "<<b+1<<endl;
int lena=strlen(a+);
int lenb=strlen(b+);
for(int i=;i<=lena;i++){
Hash[][i]=Hash[][i-]*base+a[i]-'a';
}
for(int i=;i<=lenb;i++){
Hash[][i]=Hash[][i-]*base+b[i]-'a';
}
kmp(a+,b+,);
kmp(b+,a+,);
for(int i=;i<lena;i++){
cout<<ans[i];
}cout<<endl;
}
}
 

最新文章

  1. MAC usb启动盘制作
  2. HTML5+ 拍照上传 和选择文件上传
  3. DOTA2参数收集
  4. Android 编程下的代码混淆
  5. PEM文件格式详细解析
  6. [设计模式] 9 装饰者模式 Decorator
  7. DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法
  8. 简单改造 starling 中的 AssetManager 让其更适合 批次加载纹理
  9. R文件相关(坑)
  10. Python开发【第一篇】:目录
  11. RMQ之ST算法
  12. 在windows XP系统下编译和使用ffmpeg
  13. #Java学习之路——基础阶段二(第四篇)
  14. JavaScript基础知识(Math的方法)
  15. 使用Visual Studio Installer 2015打包WPF程序
  16. 洛谷P1119灾后重建
  17. SQL新增数据取表主键最新值
  18. 免费SSL证书(https网站)申请
  19. MyEclipse WebSphere开发教程:WebSphere 7安装指南(二)
  20. [UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例

热门文章

  1. [Python3网络爬虫开发实战] 6.2-Ajax分析方法
  2. selenium实战演练
  3. poj 3253 Fence Repair (优先队列,哈弗曼)
  4. nodejs的express框架创建https服务器
  5. String类的概述和构造方法
  6. fd最大值和限制
  7. Xcode4.5.1破解iOS免证书开发真机调试与ipa发布
  8. memcache 原理 &amp; 监测 &amp; 查看状态 &amp; stats &amp; 结构
  9. mysql pager用法&amp;命令行命令
  10. 【ZJOI2017 Round1练习&amp;BZOJ5354】D7T3 room(DP)