hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String
hihocoder 1084 : http://hihocoder.com/problemset/problem/1084
北京邀请赛 Just String http://www.bnuoj.com/v3/problem_show.php?pid=34990
两道题同样的做法,题目基本内容是找到A的字串中和B串长度一样,且不同的字符个数不超过k个的置。
以hihocoder 1084为例, 是求有多少个A的字串的,与B串长度一样,且不同的字符个数不超过k。
分析:预处理hash,然后对每个字串进行判断,例如A[i~i+len-1] 和B[0~len-1], 首先找到一个A,B都相同的字符,然后从此出发找到长度尽可能大的完全相同的一截,这里可以用二分完成,然后再次找到一个对应位置A,B相同的字符,一旦不相同的字符个数超过k则立马终止,这样复杂度接近O(N*logN*k)。
代码:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define esp 1e-14
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define sz(x) ((int)((x).size()))
#define pf(x) ((x)*(x))
#define pb push_back
#define pi acos(-1.0)
#define in freopen("solve_in.txt", "r", stdin);
#define bug(x) cerr << "Line : " << (x) << " >>>>>>\n";
#define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef map<LL, int> MPS;
typedef pair<int, int> PII;
typedef MPS::iterator IT; const int maxn = + ;
const int B = (int)1e6 + ; char sa[maxn], sb[maxn];
ULL pw[maxn], ha[maxn], hb[maxn];
int n, m, k;
void pre(){
pw[] = ;
for(int i = ; i < maxn; i++)
pw[i] = pw[i-]*B;
}
inline ULL cal(ULL h[], int l, int r, int len){
return h[l]-h[r+]*pw[len];
}
inline int idx(char ch){
return ch - 'a';
}
int main() { pre();
while(scanf("%s%s%d", sa, sb, &k) == ){
n = strlen(sa);
m = strlen(sb);
ha[n] = hb[m] = ;
for(int i = n-; i >= ; i--)
ha[i] = ha[i+]*B+idx(sa[i]);
for(int i = m-; i >= ; i--)
hb[i] = hb[i+]*B+idx(sb[i]);
int ans = ;
for(int i = ; i <= n-m; i++){
int j = , x = ;
// bug(i)
while(x <= k && j < m){
while(j < m && sa[i+j] != sb[j] && x <= k)
j++, x++;
// bug(j)
if(j >= m || x > k) break;
int l = j, r = m;
while(l+ < r){
int mid = (l+r)>>;
// bug(mid)
ULL h1 = cal(ha, i+l, i+mid, mid-l+);
ULL h2 = cal(hb, l, mid, mid-l+);
if(h1 == h2)
l = mid;
else r = mid;
}
if(l != m-)
x++;
j = l+;
}
ans += x <= k;
}
printf("%d\n", ans);
}
return ;
}
北京邀请赛同样的分析方法。
代码:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define esp 1e-14
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define sz(x) ((int)((x).size()))
#define pf(x) ((x)*(x))
#define pb push_back
#define pi acos(-1.0)
#define in freopen("solve_in.txt", "r", stdin);
#define bug(x) cerr << "Line : " << (x) << " >>>>>>\n";
#define TL cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef map<LL, int> MPS;
typedef pair<int, int> PII;
typedef MPS::iterator IT; const int maxn = + ;
const int B = (int)1e6 + ; char sa[maxn], sb[maxn];
ULL pw[maxn], ha[maxn], hb[maxn];
int n, m, k;
void pre() {
pw[] = ;
for(int i = ; i < maxn; i++)
pw[i] = pw[i-]*B;
}
inline ULL cal(ULL h[], int l, int r, int len) {
return h[l]-h[r+]*pw[len];
}
inline int idx(char ch) {
return ch - 'a';
}
int main() { pre();
int T;
for(int t = scanf("%d", &T); t <= T; t++) {
k = ;
scanf("%s%s", sa, sb);
n = strlen(sa);
m = strlen(sb);
ha[n] = hb[m] = ;
for(int i = n-; i >= ; i--)
ha[i] = ha[i+]*B+idx(sa[i]);
for(int i = m-; i >= ; i--)
hb[i] = hb[i+]*B+idx(sb[i]);
int ans = -;
for(int i = ; i <= n-m; i++) {
int j = , x = ;
// bug(i)
while(x <= k && j < m) {
while(j < m && sa[i+j] != sb[j] && x <= k)
j++, x++;
// bug(j)
if(j >= m || x > k) break;
int l = j, r = m;
while(l+ < r) {
int mid = (l+r)>>;
// bug(mid)
ULL h1 = cal(ha, i+l, i+mid, mid-l+);
ULL h2 = cal(hb, l, mid, mid-l+);
if(h1 == h2)
l = mid;
else r = mid;
}
if(l != m-)
x++;
j = l+;
}
if(x <= k) {
ans = i;
break;
}
}
printf("Case #%d: %d\n", t, ans);
}
return ;
}
hihocoder 1084 扩展KMP && 2014 北京邀请赛 Justice String的更多相关文章
- 2014 北京邀请赛ABDHJ题解
A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...
- BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...
- 2014 ACM/ICPC 北京邀请赛 部分 题解
题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...
- 扩展KMP算法
一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- KMP和扩展KMP
文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...
- UVA5876 Writings on the Wall 扩展KMP
扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...
- hdu4333 扩展KMP
慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...
- 扩展KMP
刘雅琼论文 http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 论文讲的非常详细. 给定母串S,子串T,n=strlen(S),m=st ...
随机推荐
- div层遮盖flash(兼容浏览器)
今天测试div层和flash的交互,发现div层总是被flash层遮盖,在百度上找了一会,说是加个<param name="wmode" value="transp ...
- ArcGIS API for JavaScript介绍
ArcGIS API for JavaScript中的类是按照模块组织的,主要包含esri.esri/geometry.esri/renderers.esri/symbols.esri/symbols ...
- 基础学习总结(四)---内存获取、XML之PULL解析
外部项目引入如果没找到工程文件要先创建一个android项目在引入. 根据要查看的项目名在项目中搜索逐步查看 ==================== 内存详情获取: public class Mai ...
- [大牛翻译系列]Hadoop(17)MapReduce 文件处理:小文件
5.1 小文件 大数据这个概念似乎意味着处理GB级乃至更大的文件.实际上大数据可以是大量的小文件.比如说,日志文件通常增长到MB级时就会存档.这一节中将介绍在HDFS中有效地处理小文件的技术. 技术2 ...
- CSS居中的实现用法实例
转载的一篇文章,讲解css内容居中的. 网上有关css 居中的文章不胜枚举,不过大多没有做系统的总结.这里分享的这篇有关css居中的文章,个人感觉不错,值得收藏. 一.水平居中1,将元素水平居中(us ...
- Posix 信号量
作用 信号量的值为0或正整数,就像红灯与绿灯,用于指示当前是否可以接受任务. 信号量对进程和线程都适用. gcc编译时需加-lpthread 基本函数 信号量的相关函数与标准文件函数非常相似,可以理解 ...
- CentOS6.3连网获取IP失败 This device is not active
虚拟机拷贝到其它机器之后,启动:然后用ifconfig -a发现eth0没有IP地址,查看 /etc/sysconfig/network-scripts/ifcfg-eth0文件,发现IP地址已经指定 ...
- 小甲鱼汇编语言006第二章 寄存器(CPU工作原理)01
http://baidu.ku6.com/watch/09215216064281951074.html?page=videoMultiNeed
- 复习linq
复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...
- Careercup - Facebook面试题 - 4909367207919616
2014-05-01 01:23 题目链接 原题: WAP to modify the array such that arr[I] = arr[arr[I]]. Do this in place i ...