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的更多相关文章

  1. 2014 北京邀请赛ABDHJ题解

    A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...

  2. BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...

  3. 2014 ACM/ICPC 北京邀请赛 部分 题解

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem.php?search=2014+ACM-ICPC+Beijing+Invitational+Programming+C ...

  4. 扩展KMP算法

    一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...

  5. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  6. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  7. UVA5876 Writings on the Wall 扩展KMP

    扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...

  8. hdu4333 扩展KMP

    慢慢研究可以发现,可以用扩展kmp来求.由于扩展kmp的next[]只有一部分,当前位子前面那部分和母串的后部分,所以可以将字符串复制接在后面一次. 先求如果next[]>0&& ...

  9. 扩展KMP

    刘雅琼论文 http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 论文讲的非常详细. 给定母串S,子串T,n=strlen(S),m=st ...

随机推荐

  1. 用户不在sudoers文件中的解决方法

    1.更改/etc/sudoers权限为777 2.修改文件中 root    ALL=(ALL)       ALL 下方增加 user    ALL=(ALL)       ALL 3.回复文件/e ...

  2. JQuery window.opener

    $('#Save').click(function () {    var parent = $(parent.document.body);    $(parent).find('input#add ...

  3. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...

  4. PHP弱类型安全问题笔记

    一.类型转换问题    intval(); var_dump(intval('1asdfasd'));  //1 var_dump(intval('awqw12'));  //0 var_dump(i ...

  5. opencv的初体验

    http://guoming.me/opencv-config  这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...

  6. require.js入门指南(二)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  7. python之类私有成员

    python类的成员前加双下划线"__", 则被看作"私有"成员. 实例不能简单地通过<instance>.<name>来访问. 但py ...

  8. 开发一个iOS应用没有那么容易

    导读:这是来自新加坡的 iOS 开发者 Kent Nguyen 发表在1月底的一篇博文.这篇吐槽文在 iOS 开发圈子里流传甚广,从原文150多个评论就可见一斑,现翻译如下. 让我们开门见山吧:做一个 ...

  9. 标签跳转break和continue

    标签是后面跟有冒号的标识符,例如  label1: 在java中,标签起作用的唯一的地方刚好是在迭代语句之前. “刚好之前”的意思表明,在标签和迭代之间置入热和语句都不好. 而在迭代之前设置标签的唯一 ...

  10. 设计模式Builder(建造者)模式

    1.出现原因 在软件系统中,有时候会面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂的对象的各个部分可能面临着剧烈的变化,但是把他们组合在一起的算法 ...