Justice String

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 题意:两个字符串, 求B在在A串出现的第一个位置, 可以允许最多两个字符不同。
做法: 依次枚举位置i, 然后求A[i, ...lena - 1]与B的最长公共前缀, 再求 A[i, i+lenb-1]的B的最长公共后缀。。 然后对于中间那一部分, 用多项式hash直接判断是否相等。
 #include <bits/stdc++.h>
using namespace std;
const int seed = 1e9+;
const int MAXN = 1e5+;
typedef unsigned long long uLL;
uLL _hash[][MAXN];
string s1, s2;
void Hash(string s, int d){
int len = s.size();
memset(_hash[d], , sizeof (_hash[d]));
for (int i = ; i < len; i++){
_hash[d][i] = (i ? _hash[d][i-] : ) * seed + s[i] - '';
}
}
void pre_kmp(string &s, int m, int next[]){
next[] = m;
int j = ;
while (j + < m && s[j] == s[j+]){
j++;
}
next[] = j;
int k = ;
for (int i = ; i < m; i++){
int p = next[k] + k - ;
int L = next[i-k];
if (i + L < p + ){
next[i] = L;
}else{
j = max(, p-i+);
while (i+j < m && s[i+j] == s[j]){
j++;
}
next[i] = j;
k = i;
}
}
}
void exkmp(string &str1, int len1, string &str2, int len2, int next[], int extend[]){
pre_kmp(str1, len1, next);
int j = ;
while (j < len2 && j < len1 && str1[j] == str2[j]){
j++;
}
extend[] = j;
int k = ;
for (int i = ; i < len2; i++){
int p = extend[k] + k - ;
int L = next[i-k];
if (i + L < p + ){
extend[i] = L;
}else{
j = max(, p-i+); while (i+j < len2 && j < len1 && str2[i+j] == str1[j]){
j++;
}
extend[i] = j;
k = i;
}
}
}
int ex1[MAXN], ex2[MAXN], next[MAXN];
uLL bas[MAXN];
void pre_solve(){
bas[] = ;
for (int i = ; i < MAXN; i++){
bas[i] = bas[i-] * seed;
}
}
int main(){ //freopen("in.txt", "r", stdin);
pre_solve();
int T, cas = ;
scanf ("%d", &T);
while (T--){
cin >> s1 >> s2;
int len1 = s1.size();
int len2 = s2.size();
Hash(s1, );
Hash(s2, );
string tmp1 = s1;
string tmp2 = s2;
reverse(tmp1.begin(), tmp1.end());
reverse(tmp2.begin(), tmp2.end());
exkmp(s2, len2, s1, len1, next, ex1);
exkmp(tmp2, len2, tmp1, len1, next, ex2);
int ans = -;
for (int i = ; i < len1; i++){
int t1 = ex1[i];
int t2 = ex2[len1 - i - len2];
int L1 = i+t1, R1 = i+len2-t2-;
int L2 = t1, R2 = len2-t2-;
bool ok1 = ((t1 == len2) || (R1 <= L1+)); uLL p1 = (_hash[][R1-] - _hash[][L1] * bas[R1--L1]);
uLL p2 = (_hash[][R2-] - _hash[][L2] * bas[R2--L2]);
bool ok2 = (p1 == p2);
if (ok1 || ok2){
ans = i;
break;
}
}
printf("Case #%d: %d\n", cas++, ans);
}
return ;
}

BNUOJ34990--Justice String (exkmp求最长公共前缀)的更多相关文章

  1. 求最长公共前缀和后缀—基于KMP的next数组

    KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...

  2. leetcode 14 最长公共前缀

    描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...

  3. LeetCode 14 Longest Common Prefix(最长公共前缀)

    题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description   Problem: 找出给定的string数组中最 ...

  4. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  5. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

  6. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. 文本比较算法Ⅱ——Needleman/Wunsch算法的C++实现【求最长公共子串(不需要连续)】

    算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html 求最长公共子串(不需要连续) #include <stdio. ...

  8. HDU 1243 反恐训练营 (动态规划求最长公共子序列)

    反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  9. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

随机推荐

  1. 加入gitignore文件没有起作用怎么办

    步骤一: 假设有未提交的文件先提交到Git. 步骤二: 在Git根文件夹下运行以下的Git命令: git rm -r --cached . git add . git commit -m " ...

  2. Sandcastle Help File Builder使用教程

    Sandcastle Help File Builder相信很多的园友用过,小弟我最近因为工作原因需要生成公司的一套SDK的帮助文档,因此找了一些资料,发现网上的资料很多,但是都不怎么完全,有些只是随 ...

  3. View原理

    View处理: 绘制(paint canvas path:tween等动画效果).事件处理   参考整理自: Custom Components: http://developer.android.c ...

  4. wildcard

    [rusky@rhel7 test]$ lstest1  test123  test2  test317  test33  test335  test336  test44  testtest[rus ...

  5. DiskLruCache 硬盘缓存 使用简介

    简介 LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时.对此,Google又提供了一套硬盘缓存的解决方案:DiskLru ...

  6. python的运算符

    #coding=utf-8#"+"两个对象相加#两个数字相加a=7+8print a #两个字符串相加b="GOOD"+"JOB"print ...

  7. MyEclipse Web Project导入Eclipse Dynamic Web Project,无法部署到tomcat问 题

    做作业遇到一个小问题,将MyEclipse Web Project导入到Eclipse中开发.在部署到tomcat时,发现无法发布这个项目. 问题分析: MyEclipse Web Project被识 ...

  8. sql设置事务隔离级别

    SET TRANSACTION一共有以下几种级别: SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPE ...

  9. 如何清理多余的Windows桌面右键菜单

    删除多余的发送到选项 Win7使用一段时间后,我们可能会装很多软件,这时候右键菜单可能会变得很长,特别是“发送到(Send to)”里面的选项,有时候我们卸载软件之后在右键发送到菜单里还会有残存的选项 ...

  10. a标签增加onclick事件提示未定义function

    项目使用的是ext框架,版本是ext4.2 出现的问题代码如下: renderer : function(value){ var html = "<a href=\"java ...