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. VIM小技巧之文件名补全

    恩,这两天在看<简明Python教程>,那里面作者建议写代码的时候前面的注释写上文件名,写上调用的解释器,比如这样: 恩,然后我当然不可能每回新建一个文件,就要在开头写上一大串东西啊,vi ...

  2. CentOS 7 用户账户配置

    说明: 1.这篇博文记录的是CentOS 7 用户账户的配置,包括添加用户.添加用户组.删除用户.删除用户组等.其中包括分析用户的配置文件.目录以及对安全的思考. 2.用户配置方面CentOS 7与以 ...

  3. [Android教程]TextView使用SpannableString设置复合文本

    TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来对指定文本进行相关处理,具体有以下功能: 1.Bac ...

  4. 利用ddmlib 实现 PC端与android手机端adb forword socket通信(转)

    上篇文章讲了PC与android手机连接的办法 ,通过java调用系统命令执行adb命令操作,实际上是一个比较笨的办法. 网上查阅资料,发现google 提供了ddmlib库 (adt-bundle\ ...

  5. Zendframework application 引导过程

    Applications 会期望用户提供一个配置好的ServiceManager.提供以下服务: 1.EventManager 2.ModuleManager 3.Request 4.Response ...

  6. pipe/popen/fifo

    pipe(管道) 专用于父子进程通信, 函数原型 int pipe(int fd[2]) fd[0]表示输入, fd[1]表示输出 如果父子进程要双向通信, 可以通过类似信号的功能进行控制, 也可以简 ...

  7. I/O空间映射

    转自:http://www.cnblogs.com/hydah/archive/2012/04/10/2232117.html 注:部分资料和图片来源于网络,本文在学习过程中对网络资源进行再整理. I ...

  8. Linux C C语言库的创建和调用

    C语言库的创建和调用 简介: 假如,你有一个庞大的工程,代码量达到数百兆甚至是数G,你经常会遇到好多重复或常用的地方.每次使用到这些地方时如果都重新写一份基本相同的代码,这当然可以,不过这样会大大地降 ...

  9. 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)

    目录[-] 一.TCP/IP协议 二.TCP与UDP 三.Socket是什么 四.Java中的Socket 五.基本的Client/Server程序 六.多客户端连接服务器 七.信息共享 八.文件传输 ...

  10. Netsharp快速入门(之12) 销售管理(开发发货单工作区)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 4.4     发货单 4.4.1  部件工作区设置 1.设置部件工作区,需要设置的部件如下 2.设置单据和明细列表的字段,设置完成 ...