Justice String

Time Limit: 2000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 

Given two strings A and B, your task is to find a substring of A called justice string, which has the same length as B, and only has at most two characters different from B.

 

Input

The first line of the input contains a single integer T, which is the number of test cases.
For each test case, the first line is string A, and the second is string B.
Both string A and B contain lowercase English letters from a to z only. And the length of these two strings is between 1 and 100000, inclusive. 
 
 

Output

For each case, first output the case number as "Case #x: ", and x is the case number. Then output a number indicating the start position of substring C in A, position is counted from 0. If there is no such substring C, output -1.
And if there are multiple solutions, output the smallest one. 
 

Sample Input

  1. 3
  2. aaabcd
  3. abee
  4. aaaaaa
  5. aaaaa
  6. aaaaaa
  7. aabbb

Sample Output

  1. Case #1: 2
  2. Case #2: 0
  3. Case #3: -1

Source

 
解题:Hash+二分
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. const int maxn = ;
  18. char sa[maxn],sb[maxn];
  19. unsigned LL Ha[maxn],Hb[maxn],p = ;
  20. int len1,len2;
  21. unsigned LL pp[maxn];
  22. int calc(int a,int b){
  23. int high = min(len1 - a,len2 - b),low = ,mid,ans = ;
  24. while(low <= high){
  25. mid = (low + high)>>;
  26. unsigned LL v1 = Ha[a] - Ha[a+mid]*pp[mid];
  27. unsigned LL v2 = Hb[b] - Hb[b+mid]*pp[mid];
  28. if(v1 == v2){
  29. ans = mid;
  30. low = mid + ;
  31. }else high = mid - ;
  32. }
  33. return ans;
  34. }
  35. int main() {
  36. int T,cs = ;
  37. pp[] = ;
  38. for(int i = ; i < maxn; ++i) pp[i] = pp[i-]*p;
  39. scanf("%d",&T);
  40. while(T--){
  41. scanf("%s %s",sa,sb);
  42. len1 = strlen(sa);
  43. len2 = strlen(sb);
  44. Ha[len1] = ;
  45. Hb[len2] = ;
  46. for(int i = len1-; i >= ; --i) Ha[i] = Ha[i+]*p + sa[i];
  47. for(int i = len2-; i >= ; --i) Hb[i] = Hb[i+]*p + sb[i];
  48. int ans = -;
  49. for(int i = ; i <= len1 - len2; ++i){
  50. int sum = ,a = i,b = ,tmp = ;
  51. tmp = calc(a,b);
  52. if(tmp >= len2-){
  53. ans = i;
  54. break;
  55. }
  56. sum += tmp;
  57. a += tmp+;
  58. b += tmp+;
  59. tmp = calc(a,b);
  60. sum += tmp;
  61. if(sum >= len2-){
  62. ans = i;
  63. break;
  64. }
  65. a += tmp+;
  66. b += tmp+;
  67. tmp = calc(a,b);
  68. sum += tmp;
  69. if(sum >= len2-){
  70. ans = i;
  71. break;
  72. }
  73. }
  74. printf("Case #%d: %d\n",cs++,ans);
  75. }
  76. return ;
  77. }

BNUOJ 34990 Justice String的更多相关文章

  1. BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990 DEBUG了非常久,还是legal的推断函数写错了... 此题做法.枚举Stri ...

  2. BNU 34990 Justice String (hash+二分求LCP)

    思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  3. bnuoj 34990(后缀数组 或 hash+二分)

    后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀. 我自己在想hash的时候一直在考虑hash成数值时MOD ...

  4. hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String

    hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just  String http://www.bnuoj.co ...

  5. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  6. BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...

  7. BNUOJ34990--Justice String (exkmp求最长公共前缀)

    Justice String Given two strings A and B, your task is to find a substring of A called justice strin ...

  8. bunoj 34990(hash)

    传送门:Justice String 题意:有两个串A,B,问是否存在A的一个子串S,S和B的长度相等,最多有2个字符不同.如果有多个,输出其实下标最小S的下标,没有输出-1. 分析:从A每个位置开始 ...

  9. 2014 ACM/ICPC 北京邀请赛 部分 题解

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...

随机推荐

  1. poi API大全

    一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二. HSSF概况 HSSF 是 ...

  2. Spring IOC过程

    https://www.processon.com/diagraming/5c96171fe4b0f88919b98497 1. AbstractApplicationContext:执行refres ...

  3. URAL 1457. Heating Main

    space=1&num=1457">1457. Heating Main Time limit: 1.0 second Memory limit: 64 MB Backgrou ...

  4. FZU_Problem 2168 防守阵地 I

    Problem 2168 防守阵地 I Accept: 128 Submit: 392 Time Limit: 3000 mSec Memory Limit : 32768 KB Problem De ...

  5. poj3101--Astronomy(分数的最小公倍数)

    题目链接:id=3101">点击打开链接 题目大意:有n个行星,给出每个行星的旋转的周期.问最少多少时间后n个行星会在一条直线上,初始点在一起,不存在全部的行星都有同一个周期 如果A行 ...

  6. python实战之编码问题:中文!永远的痛

    编码的思维图谱: 也就是说文件没有编码之说,事实上都是按二进制格式保存在硬盘中的.不过在写入读取时须使用相应的编码进行处理,以便操作系统配合相关软件/字体,绘制到屏幕中给人看.所以关键问题是得知道原先 ...

  7. java Semaphore信号亮-同意多个任务同一时候訪问这个资源--thinking in java21.7.6

    package org.rui.thread.newc.semaphore; import java.util.ArrayList; import java.util.List; import jav ...

  8. hdu1525 Euclid&#39;s Game , 基础博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=1525 题意: 两人博弈,给出两个数a和b, 较大数减去较小数的随意倍数.结果不能小于0,将两个数随意一个数减到0 ...

  9. [SICP] 求值规则

    在Java语言学习中,通常不太关注求值规则. (2+4*6)*(3+5+7)这样的组合式的求值规则.通常归结为优先级问题: if.for等的求值规则通常归结为语义. 函数式编程语言的Scheme,将这 ...

  10. CentOS7安装EPEL的两种方式

    转自:http://www.mamicode.com/info-detail-1671603.html epel是社区强烈打造的免费开源发行软件包版本库. EPEL,即Extra Packages f ...