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. selenium+xpath获取href的坑

    先上HTML文档 <html> <body> <a href="http://www.example.com">Example</a> ...

  2. POI 详细介绍

    Apache POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.目前POI已经有了Ruby版本. 结构: HSSF - 提供读写Microsoft Excel XLS格式 ...

  3. 今天开始看看brpc-baidurpc

    链接如下: Link 大概看了一下.没有很明显的感觉.还是喜欢机器学习相关的内容.

  4. Mysql数据库事务的隔离级别和锁的实现原理分析

    Mysql数据库事务的隔离级别和锁的实现原理分析 找到大神了:http://blog.csdn.net/tangkund3218/article/details/51753243 InnoDB使用MV ...

  5. HDU1061_Rightmost Digit【高速幂取余】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Android本地存储方案 SharedPreferences

    原文地址:http://www.yanwushu.com/post/43.html 存储位置 SharedPreferences数据保存在: /data /data/<package_name& ...

  7. Ubuntu: GlusterFS+HBase安装教程

    HBase通常安装在Hadoop HDFS上,但也能够安装在其它实现了Hadoop文件接口的分布式文件系统上.如KFS. glusterfs是一个集群文件系统可扩展到几peta-bytes. 它集合了 ...

  8. 关闭 sftp

    vi /etc/ssh/sshd_config 注释掉这行Subsystem  sftp    /usr/libexec/openssh/sftp-server /etc/rc.d/init.d/ss ...

  9. HTTP学习记录

    title: HTTP学习记录 toc: true date: 2018-09-21 20:40:48 HTTP协议,HyperText Transfer Protocol,超文本传输协议,是因特网上 ...

  10. POJ 3269 中位数

    题意: 思路: 这道题坑也不少.. 你准备好脑洞了么? 首先 要认真审题 题目中有说:"没有两头牛的吃草位置是相邻的" 这句话让我们省了很多的事儿 (Discuss里有的大神就入了 ...