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判断是否相等
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<cstring>
  7. #include<map>
  8. #include<queue>
  9. #include<stack>
  10. #include<set>
  11. #include<vector>
  12. #include<algorithm>
  13. #include<string.h>
  14. typedef long long ll;
  15. typedef unsigned long long LL;
  16. using namespace std;
  17. const int INF=0x3f3f3f3f;
  18. const double eps=0.0000000001;
  19. const int N=+;
  20. const ll mod=1e9+;
  21. const LL base=;
  22. LL p[N];
  23. LL Hash[][N];
  24. char a[N];
  25. char b[N];
  26. int Next[N];
  27. int ans[N];
  28. void init(){
  29. p[]=;
  30. for(int i=;i<=;i++)p[i]=p[i-]*base;
  31. }
  32. LL get_val(int l,int r,int t){
  33. LL ans=Hash[t][r]-Hash[t][l-]*p[r-l+];
  34. return ans;
  35. }
  36. int check(int i,int j,int t){
  37. i++;
  38. j++;
  39. if(i==j)return ;
  40. if(get_val(j+,i,t^)==get_val(,i-j,t))return ;
  41. return ;
  42. }
  43. void get_next(char *T){
  44. int len=strlen(T);
  45. Next[]=-;
  46. int j=-;
  47. for(int i=;i<len;i++){
  48. while(j!=-&&T[j+]!=T[i])j=Next[j];
  49. if(T[j+]==T[i])j++;
  50. Next[i]=j;
  51. }
  52. }
  53. void kmp(char *S,char *T,int t){
  54. int lens=strlen(S);
  55. int lent=strlen(T);
  56. get_next(T);
  57. // for(int i=0;i<lent;i++)cout<<Next[i]<<" ";
  58. //cout<<endl;
  59. int j=-;
  60. for(int i=;i<lens;i++){
  61. while(j!=-&&T[j+]!=S[i])j=Next[j];
  62. if(S[i]==T[j+]){
  63. j++;
  64. //cout<<i<<" "<<j<<endl;
  65. if(ans[i]==){
  66. ans[i]=check(i,j,t);
  67. }
  68. }
  69. }
  70. }
  71. int main(){
  72. init();
  73. while(scanf("%s%s",a+,b+)!=EOF){
  74. memset(ans,,sizeof(ans));
  75. //cout<<a+1<<" "<<b+1<<endl;
  76. int lena=strlen(a+);
  77. int lenb=strlen(b+);
  78. for(int i=;i<=lena;i++){
  79. Hash[][i]=Hash[][i-]*base+a[i]-'a';
  80. }
  81. for(int i=;i<=lenb;i++){
  82. Hash[][i]=Hash[][i-]*base+b[i]-'a';
  83. }
  84. kmp(a+,b+,);
  85. kmp(b+,a+,);
  86. for(int i=;i<lena;i++){
  87. cout<<ans[i];
  88. }cout<<endl;
  89. }
  90. }
 

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. 零基础入门学习Python(25)--字典:当索引不好用时

    知识点 字典属于映射类型. 列表,元祖,字符串等属于序列类型 创建及访问字典 #创建一个字典 >>> dict1 = {'李宁':'一切皆有可能','耐克':'Just do it' ...

  2. PHP典型功能与Laravel5框架开发学习笔记

    步骤一:PHP的Redis应用及HTTP协议 一.Redis初识 1.Linux下安装redis:具体看官网:https://redis.io/download:以下为以个人习惯的安装目录进行的red ...

  3. Python数据类型方法

    Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: ...

  4. 自媒体人Chrome浏览器必备插件精选神器!

    自从互联网时代起,浏览器使用从最早的IE,到opera,到猎豹浏览器,到360双核浏览器,到火狐,到safari,到目前最喜欢用的chrome.一路下来,chrome的稳定性与扩展性征服了我,成了我必 ...

  5. MySQL-----连表

    连表: **拿到两张表的信息** select * from userinfo,department 弊端是数据会乱,出现重复,不建议这样. **使userinfo表的part_id列与departm ...

  6. Java线上应用故障排查

    线上故障主要2种: CPU利用率很高, 内存占用率很大 一.CPU利用率很高 1. top查询那个进程CPU使用率高 2. 显示进程列表 ps -mp pid -o THREAD,tid,time 找 ...

  7. Symmetry

    Description The figure shown on the left is left-right symmetric as it is possible to fold the sheet ...

  8. hihoCoder#1120 小Hi小Ho的惊天大作战:扫雷·三

    原题地址 看上去非常复杂, 实际上是这一系列最简单的一步,本质上是个搜索过程,相比于前一道题,可以不用策略三,而且题目的数据规模超级小,所以暴力搜索就能过. 把尚未确定的点放在一个unsettled列 ...

  9. codeforces 363B

    #include<stdio.h> #include<string.h> #define inf 999999999 #define N 151000 int a[N],c[N ...

  10. Linux下汇编语言学习笔记21 ---

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