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. Linux下启动tomcat报java.lang.OutOfMemoryError: PermGen space

    一.错误信息 java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke ...

  2. Servlet中的几个重要的对象(转)

    讲解四大类,ServletConfig对象,ServletContext对象.request对象,response对象 ServletConfig对象 获取途径:getServletConfig(); ...

  3. Quartz --Scheduler

  4. selenium IDE断言设置实践

    断言: 验证应用程序的状态是否同所期望的一致. 常见的断言包括:验证页面内容,如标题是否为X或当前位置是否正确等等. 断言被用于4种模式+5种手段: Assert Assert 断言失败时,该测试将终 ...

  5. MySQL最优配置文件模板·2016-11-28

    小伙伴们大爱的MySQL最优配置文件模板更新啦.对之前的MySQL最优配置文件·20160901做了一些修正,更为名至实归.可以通过此链接进行下载.当然,更欢迎同学们提出意见和建议,共同打造一个最优M ...

  6. csu - 1538: Shopping (贪心)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1538 很奇妙的一个题,开始没有思路.问了别人才知道. 题目的意思可以理解成上图中,从0点开始向右走 ...

  7. ZOJ3953 ZJU2017校赛(贪心)

    题意:给出n个区间,求至少删掉多少个区间使得不存在区间a, b, c 两两相交    (定义两个区间相交是,区间[l1, r1]和区间[l2, r2]相交,当且仅当存在一个数x,l1<=x< ...

  8. 断路器监控(Hystrix Dashboard)

    继上一篇http://www.cnblogs.com/EasonJim/p/7613595.html介绍了断路器之后,其实它还提供了一个管理页面来监控这些应用的调用数据. 首先,我是基于上一个例子Zo ...

  9. Java正则表达式过滤出字母、数字和中文

    原文:http://blog.csdn.net/k21325/article/details/54090066 1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A ...

  10. Linux 命令修改系统时间

    修改linux的系统时间使用date指令,date命令的功能是显示和设置系统日期和时间. 输入date 查看目前系统时间. 修改时间需要 date -功能字符 修改内容 命令中各选项的含义分别为:-d ...