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 ...
随机推荐
- Qemu之Network Device全虚拟方案一:前端网络流的建立
KVM在I/O虚拟化方面,传统的方式是使用Qemu纯软件的方式来模拟I/O设备,当中包含常常使用的网卡设备.这次我们重点分析Qemu为实现网络设备虚拟化的全虚拟化方案.本主题从三个组成方面来完整描写叙 ...
- Linux crontab 命令详解(含配置文件路径)
编辑/etc/crontab 文件配置cron cron 服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服 ...
- struts2,hibernate,spring整合笔记(1)
今天终于配置好了ssh框架的整合,记录下过程供参考 环境:window8.1,jdk1.7 ,带有javaee的eclipse,也就是说要能发布web项目,TOMCAT服务器,tomcat配置涉及到环 ...
- [转] 深入剖析 linux GCC 4.4 的 STL string
本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...
- STL之auto_ptr
What's auto_ptr? The auto_ptr type is provided by the C++ standard library as a kind of a smart poin ...
- linux elinks命令
Elinks是基于文本的免费浏览器,用于Unix及基于Unix的系统.Elinks支持 HTTP,HTTP Cookies以及支持浏览Perl和Ruby脚本.也很好的支持选项卡浏览.最棒的是它支持鼠标 ...
- 关于安装PHP补装PDO与PDO_MYSQL操作
我这里是通过PHP源码包来安装的 1.安装pdo cd到你的PHP源码包下的ext/pdo目录,然后执行如下操作: #/usr/local/php/bin/phpize (/usr/local/p ...
- 渲染器 Shader BitmapShader
渲染模式: tileX tileY:The tiling mode for x/y to draw the bitmap in. 在位图上 X/Y 方向 瓦工/花砖/瓷砖 模式 CLAMP :如 ...
- maven第7章生命周期和插件
maven插件用到哪些思想? 7.7 从命令行调用插件 目标前缀和插件前缀是一个意思. 在本地搭建maven环境,熟悉maven的环境.
- activiti总结
1.activiti如何修改登录用户名?在哪个数据库里面添加. 2.activiti的启动和部署在http://activiti.org/userguide/index.html#demo.setup ...