codeforces MUH and Cube Walls
题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得
a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k
就说b串在a串中出现过!最后输出b串在a串中出现几次!
思路: KMP变形!如何转换成KMP求解呢?
举一个例子说明一下:
a: 5 10 8 10 11 9 11 12 10 15
b: 4 2 4 5 3
根据题意 a中的 10 8 10 11 9 与 b是匹配的, 11 9 11 12 10跟b也是匹配的!
如何将b串以及 10 8 10 11 9, 以及 11 9 11 12 10转换成同一个串?这样好套用kmp啊!
因为对应的数值的差值都是相同的! 令a[i]-=a[i+1], b[i]-=b[i+1]!
这样也就是将串的长度减少了1,这样就可以套kmp模板了!
别忘记对特殊情况考虑一下!
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 200005
using namespace std; int f[N], a[N], b[N];
int n, w;
void getFail(){
f[]=; f[]=;
for(int i=; i<w; ++i){
int j=f[i];
while(j && b[i] != b[j]) j=f[j];
if(b[i] == b[j]) f[i+] = j+;
else f[i+] = ;
}
} void findText(){
int j=;
int cnt = ;
for(int i=; i<n; ++i){
while(j && a[i] != b[j]) j=f[j];
if(a[i] == b[j]) ++j;
if( j == w ) ++cnt;
}
printf("%d\n", cnt);
} int main(){
scanf("%d%d", &n, &w);
for(int i=; i<n; ++i){
scanf("%d", &a[i]);
if(i) a[i-] -= a[i];
} for(int i=; i<w; ++i){
scanf("%d", &b[i]);
if(i) b[i-] -= b[i];
}
if(n== && w==){
printf("%d\n", );
return ;
}
else if(n==){
printf("%d\n", );
return ;
}
else if( w== ){
printf("%d\n", n);
return ;
}
--n;
--w;
b[w] = -1e6;
getFail();
findText();
return ;
}
codeforces MUH and Cube Walls的更多相关文章
- Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp
D - MUH and Cube Walls Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...
- D - MUH and Cube Walls
D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...
- [codeforces471D]MUH and Cube Walls
[codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...
- CodeForces 471D MUH and Cube Walls -KMP
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...
- Codeforces 471 D MUH and Cube Walls
题目大意 Description 给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化. Input 第一行给出数字N.N在[2,1000000 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- CF471D MUH and Cube Walls
Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...
随机推荐
- 每天一个linux命令(4):mkdir命令
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...
- com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: current time:
http://www.cnblogs.com/mayongsheng/p/4387109.html https://github.com/awslabs/aws-sdk-android-samples ...
- 如何在windows7上安装启明星系统。
启明星系统提供多种安装方式.安装包里自带了setup.exe.每个程序的 install下有在线安装(例如请假应用程序为book,则默认为 http://localhost/book/install ...
- 如何获得WPA握手包&EWSA破解WPA密码教程[zz]
获得WPA\WPA2 握手包的方法有很多,下面介绍通过aircrack-ng工具包来载获握手数据包. 1.下载aircrack-ng工具包. 2.终端里输入 sudo airmon-ng start ...
- iOS 渐变提示。Labe自动换行
UILabel *noticeLabel = [[UILabel alloc]init]; noticeLabel.text = @"当前账号已失效果,已清除当前账号已失效果,已清除当前账号 ...
- MySql 执行语句错误 Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
关于用Power Designer 生成sql文件出现 错误 [Err] 1064 - You have an error in your SQL syntax; check the manual ...
- ECshop 怎样修改商品详细页的“浏览次数”
怎样修改商品详细页的“浏览次数” 最好可以修改为成倍增加的,比如客户浏览了一次,显示的是20次. 修改 goods.php 文件的下面这行代码即可 $db->query('UPDATE ' . ...
- [转载] Calculating Entropy
From: johndcook.com/blog For a set of positive probabilities pi summing to 1, their entropy is defi ...
- 基本的文件 I/O
基本的文件 I/O MSDN 抽象基类 Stream 支持读取和写入字节.Stream 集成了异步支持.其默认实现根据其相应的异步方法来定义同步读取和写入,反之亦然. 所有表示流的类都是从 Strea ...
- MD5 加密的两种方法
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5Cryp ...