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;
}
}
 

hdu 5782(kmp+hash)的更多相关文章

  1. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  2. hdu 1496 Equations hash表

    hdu 1496 Equations hash表 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1496 思路: hash表,将原来\(n^{4}\)降 ...

  3. Codeforces 1090J $kmp+hash+$二分

    题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...

  4. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  5. HDU 5782 Cycle(KMP+Hash)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5782 [题目大意] 给出两个字符串,判断他们每一个前缀是否循环同构,循环同构的意思就是,字符串首位 ...

  6. HDU 5782 Cycle(KMP+哈希)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个长度相等的字符串,输出两个字符的每个前缀是否循环相同. 思路: 如果连个串循环相 ...

  7. HDU 5782 Cycle —— KMP

    题目:Cycle 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5782 题意:给出两个字符串,判断两个字符串的每一个前缀是否循环相等(比如abc 和 ca ...

  8. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  9. 2013 Asia Regional Changchun I 题,HDU(4821),Hash

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...

随机推荐

  1. cookie和session的区别及session的生命周期

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录 ...

  2. LVS-NAT负载均衡PHP应用(Wordpress、Discuz)

    1 实验拓扑 2 需求 RS-01和RS-02对外提供WEB服务. RS-01搭建LAMP,PHP通过http模块方式提供. RS-02搭建LAMP,PHP通过fpm方式提供. RS-01和RS-02 ...

  3. java 常用集合类型--以及其特性

    1:集合:   (1) Collection(单列集合)        List(有序,可重复)            ArrayList                底层数据结构是数组,查询快,增 ...

  4. Linux最常用的基础命令 上篇

    Linux最常用的基础命令个人总结 计算机基础知识 32bit和64bit系统的区别.系统运行机制 1989年python 诞生 C语言是编译型的语言,不太支持跨平台 Django 江购 32bit= ...

  5. 调试24L01经验总结

    注意的地方: 1.调试24L01的时候,信号的初始状态一定要设置,否则在设置模块的状态的时候容易出错.( CE=0; CSN=1; SCK=0; MOSI = 1; IRQ = 1;)IRQ设置成1是 ...

  6. Python模块:time、datetime、random、os、sys、optparse

    time模块的方法: 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. struct_time时间元组,共有九个元素组.如下图: time.localtime([secs]): ...

  7. 【ZJOI2017 Round1游记】

    DAY0: 中午12点出发,下午5点到 酒店意外豪华 晚上和MG,LYY们定了个寿司套餐 没什么学习就睡觉了 DAY1: 听说RYZ在ZJ的OIer中影响颇深 讲STL的小哥真是对不住因为我是P党 D ...

  8. Linux下汇编语言学习笔记24 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. Ubuntu 16.04开机自动挂载硬盘分区(转)

    说明:如果挂载以前旧硬盘分区时不需要第2.3步! 1.查看Linux硬盘信息: sudo fdisk -l 2.格式化新硬盘(很危险,注意操作时确定硬盘分区的位置): sudo mkfs.ext4 / ...

  10. 【SQL Server 学习系列】-- SQL查询数据库表字段值不为空或Null的所有列

    ) set @TableName = 'Agency' -- 表名 declare @querySql nvarchar(max) set @querySql = 'select ' ) declar ...