BNUOJ34990--Justice String (exkmp求最长公共前缀)
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
Output
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求最长公共前缀)的更多相关文章
- 求最长公共前缀和后缀—基于KMP的next数组
KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...
- leetcode 14 最长公共前缀
描述: 给个字符串vector,求最长公共前缀. 解决: 直接取第一个字符串作为最长公共前缀,将其每个字符遍历过一次.设最长字符实际为k,共n个元素,则复杂度O(nk) string longestC ...
- LeetCode 14 Longest Common Prefix(最长公共前缀)
题目链接:https://leetcode.com/problems/longest-common-prefix/?tab=Description Problem: 找出给定的string数组中最 ...
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 文本比较算法Ⅱ——Needleman/Wunsch算法的C++实现【求最长公共子串(不需要连续)】
算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html 求最长公共子串(不需要连续) #include <stdio. ...
- HDU 1243 反恐训练营 (动态规划求最长公共子序列)
反恐训练营 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
随机推荐
- iPhone 各版本屏幕分辨率
参考:http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- [转] 智能指针(三):unique_ptr使用简介
PS: 1. auto_ptr太不安全,可能多个auto_ptr指向一个对象,出现重复释放的问题 2. unique_ptr解决了这个问题,不允许拷贝构造函数和赋值操作符,但是!它支持移动构造函数,通 ...
- C#中MD5加密
C#中进行MD5加密需要使用MD5这个类,这个类位于System.Security.Cryptography命名空间. 转到元数据得知MD5是抽象类和两个静态方法 上代码详解: //得到其静态方法创建 ...
- C# 在右下角弹出窗口
窗口代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- hdu 2304
题意: 插座插空问题 水题.....只要知道最后一个不需要插即可.... 直接贴代码.. AC代码: #include <iostream> using namespace std; in ...
- C++标准库<string>简单总结
C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...
- Error prompt:“xxx is not in the sudoers file”----Solution
//Situation System prompts "xxx is not in the sudoers file"(xxx equals the user name) w ...
- MySQL数据库备份与恢复方法(转)
来源于:http://www.jb51.net/article/25686.htm 网站数据对我们对站长来说都是最宝贵的,我们平时应该养成良好的备份数据的习惯. 常有新手问我该怎么备份数据库, ...
- Winamp传统外观皮肤MusicRio发放
这款皮肤是我在2002年自己弄的,如果能给还在使用Winamp的朋友使用那就最好了. 下载地址:http://files.cnblogs.com/lzhdim/MusicRio.rar
- 常用hash函数
常用的哈希函数 通用的哈希函数库有下面这些混合了加法和一位操作的字符串哈希算法.下面的这些算法在用法和功能方面各有不同,但是都可以作为学习哈希算法的实现的例子. 1.RS 从Robert S ...