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

3
aaabcd
abee
aaaaaa
aaaaa
aaaaaa
aabbb

Sample Output

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

Source

 
解题:Hash+二分
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
char sa[maxn],sb[maxn];
unsigned LL Ha[maxn],Hb[maxn],p = ;
int len1,len2;
unsigned LL pp[maxn];
int calc(int a,int b){
int high = min(len1 - a,len2 - b),low = ,mid,ans = ;
while(low <= high){
mid = (low + high)>>;
unsigned LL v1 = Ha[a] - Ha[a+mid]*pp[mid];
unsigned LL v2 = Hb[b] - Hb[b+mid]*pp[mid];
if(v1 == v2){
ans = mid;
low = mid + ;
}else high = mid - ;
}
return ans;
}
int main() {
int T,cs = ;
pp[] = ;
for(int i = ; i < maxn; ++i) pp[i] = pp[i-]*p;
scanf("%d",&T);
while(T--){
scanf("%s %s",sa,sb);
len1 = strlen(sa);
len2 = strlen(sb);
Ha[len1] = ;
Hb[len2] = ;
for(int i = len1-; i >= ; --i) Ha[i] = Ha[i+]*p + sa[i];
for(int i = len2-; i >= ; --i) Hb[i] = Hb[i+]*p + sb[i];
int ans = -;
for(int i = ; i <= len1 - len2; ++i){
int sum = ,a = i,b = ,tmp = ;
tmp = calc(a,b);
if(tmp >= len2-){
ans = i;
break;
}
sum += tmp;
a += tmp+;
b += tmp+;
tmp = calc(a,b);
sum += tmp;
if(sum >= len2-){
ans = i;
break;
}
a += tmp+;
b += tmp+;
tmp = calc(a,b);
sum += tmp;
if(sum >= len2-){
ans = i;
break;
}
}
printf("Case #%d: %d\n",cs++,ans);
}
return ;
}

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. Vue学习之路第八篇:事件修饰符

    学习准备: ①.顾名思义,“事件修饰符”那么肯定是用来修饰事件,既然和事件有关系,那么肯定和“v-on”指令(也可简写为:@)有关系了. ②.事件修饰符有以下几类: .stop:阻止冒泡 .preve ...

  2. [luogu3195 HNOI2008] 玩具装箱TOY (斜率优化dp)

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  3. MyBatis学习总结(19)——Mybatis传多个参数(三种解决方案)

    据我目前接触到的传多个参数的方案有三种. 第一种方案  DAO层的函数方法  Public User selectUser(String name,String area); 对应的Mapper.xm ...

  4. Java实现把两个数组合并为一个的方法总结

    本文实例讲述了Java实现把两个数组合并为一个的方法.分享给大家供大家参考,具体如下: 在Java中,如何把两个String[]合并为一个? 看起来是一个很简单的问题.但是如何才能把代码写得高效简洁, ...

  5. 【转载】黑客内核:编写属于你的第一个Linux内核模块

    黑客内核:编写属于你的第一个Linux内核模块

  6. 数据库之Case When

    近期几天的工作本来组长是安排我用mindfocion画几个图,本来以为难点是这个控件的使用,可是开发的时候才发现由于数据量有点多,所以在开发的时候汇总这些信息倒是费了我许多的功夫,最后总结一下就是写了 ...

  7. Partition(hdu4651)2013 Multi-University Training Contest 5----(整数拆分一)

    Partition Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. Linux文件系统(七)---系统调用之open操作(一)

    (内核2.4.37) 一. 当我们打开一个文件的时候.须要获得文件的文件描写叙述符(前面已经说过事实上就是文件数组下标).通常是通过函数open来完毕.这个系统调用在<unistd.h>头 ...

  9. hdoj 4548 美素数 【打表】

    另类打表:将从1到n的满足美素数条件的数目赋值给prime[n],这样最后仅仅须要用prime[L]减去prime[R-1]就可以: 美素数 Time Limit: 3000/1000 MS (Jav ...

  10. ubuntu16.04安装opencl

    参考链接:https://www.jianshu.com/p/ad808584ce26 安装OpenCL OpenCL是一系列库和头文件,需要根据硬件安装对应的SDK,也就是说,如果希望使用Intel ...