BNUOJ 34990 Justice String
Justice String
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
Output
Sample Input
3
aaabcd
abee
aaaaaa
aaaaa
aaaaaa
aabbb
Sample Output
Case #1: 2
Case #2: 0
Case #3: -1
Source
#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的更多相关文章
- 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 ...
- BNU 34990 Justice String (hash+二分求LCP)
思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...
- bnuoj 34990(后缀数组 或 hash+二分)
后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀. 我自己在想hash的时候一直在考虑hash成数值时MOD ...
- hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
hihocoder 1084 : http://hihocoder.com/problemset/problem/1084 北京邀请赛 Just String http://www.bnuoj.co ...
- 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 ...
- BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...
- BNUOJ34990--Justice String (exkmp求最长公共前缀)
Justice String Given two strings A and B, your task is to find a substring of A called justice strin ...
- bunoj 34990(hash)
传送门:Justice String 题意:有两个串A,B,问是否存在A的一个子串S,S和B的长度相等,最多有2个字符不同.如果有多个,输出其实下标最小S的下标,没有输出-1. 分析:从A每个位置开始 ...
- 2014 ACM/ICPC 北京邀请赛 部分 题解
题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...
随机推荐
- windows下的ubuntu
办公用Windows确实方便,但对于开发和学习还是用Linux比较好. 在Windows下安装Linux子系统 windows10中推出了Linux子系统,这个功能对开发和学习来说真的很好,非常方便. ...
- [luogu] P4155 [SCOI2015]国旗计划(贪心)
P4155 [SCOI2015]国旗计划 题目描述 A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此 ...
- Golang-and-package-version-managment
参考文章 学习Golang之后对golang中的版本管理,包管理等机制一直没有很好的琢磨,偶然想起还是觉得很有必要进行归纳,包管理使用起来简单,无非就是install,uninstall,list等, ...
- webpack基础知识点
webpack 是一个现代的 JavaScript 应用程序的模块打包器(module bundler). 入口(Entry) webpack 将创建所有应用程序的依赖关系图表(dependency ...
- LaTeX 表格指定宽度并居中
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532269 在绘制表格的时候,对于特 ...
- ASP.NET-AJAX.FORM提交附件失败
尝试了不少时间在AJAX.FORM提交附件,发现完全不行,经过下面的这个博客的介绍,使用ajax.form.js插件提交成功,记录一下该博文网址和结论: 相关网址:http://www.cnblogs ...
- WinServer-IIS-Dynamic IP Restrictions
动态IP限制 来自为知笔记(Wiz)
- IMP-00010: 不是有效的导出文件,标题验证失败
IMP-00010: 不是有效的导出文件,标题验证失败 IMP-00000: 未成功终止导入 在google上查找了一下,大概有两种情况: 1.imp/exp的版本不对,也就是说低版本的导出,可以 ...
- 如何将MVC AREA中的某一个页设为起始页
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...
- MHA+ProxySQL 读写分离高可用
文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...