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. 根据Dockerfile创建hello docker镜像

    一.编写hello可执行c文件: 1.安装:gcc glibc glibc-static yum install -y gcc glibc glibc-static 2.编写hello.c:vim h ...

  2. LeetCode1---两数之和

    import java.util.Arrays;import java.util.HashMap;import java.util.Map; /** *功能描述 :两数之和 * @author lkr ...

  3. 转载:Django之form表单

    转载: 一.使用form类创建一个表单 先定义好一个RegForm类: forms.py from django import forms # 导入forms类 class NameForm(form ...

  4. JAVA IO中read()方法的返回值

    read()方法的作用是从输入流读取数据的下一个字节,返回的字节的值是一个0~255之间的整数.到达流的末尾返回-1. 刚开始我以为这个返回值表示的是所读取的数据的长度,可是之后在一个示例程序中发现这 ...

  5. ASP.NET获取客户端IP及MAC地址

    朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了:下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_ ...

  6. oracle基于归档的增量异地恢复 --异地新增数据文件问题

    1 rac异地基于归档文件的增量恢复   2 遇到在主库新增数据文件 后 日志恢复出现路径问题 .   错误如下 : creating datafile fno=20 name=+DATA/dataf ...

  7. DD & E-app

    DD & E-app 企业内部开发的E应用 前端 demo https://github.com/open-dingtalk docs https://open-doc.dingtalk.co ...

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

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

  9. msp430入门编程01

    msp430单片机最小系统 msp430入门学习 msp430入门编程

  10. Gym100812 L 扩展欧几里得

    L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...