HDU 4749-Parade Show(KMP变形)
题意:
给出一个母串和一个模式串求母串中最多能分成最大的子串数(每个字串中的各个数字的大小关系和模式串相同)
分析:
KMP变形匹配规则变一下即可,用当前数字和下个数字的差表示,大小关系方便匹配
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int x[],a[];
int f[];
bool cmp(int i,int c[],int j,int d[]){
if(c[i]*d[j]>)return true;
if(c[i]==&&d[j]==)return true;
return false;
}
void hand(int n,int m){
for(int i=;i<n;++i)
x[i]=x[i+]-x[i];
for(int i=;i<m;++i)
a[i]=a[i+]-a[i];
}
void getnext(int m){
int i=,j=-;
f[]=-;
while(i<m){
if(j==-||cmp(i,a,j,a)){
i++;
j++;
f[i]=j;
}
else j=f[j];
}
}
void solve(int n,int m){
getnext(m);
int i=,j=,num=;
while(i<n){
if(cmp(i,x,j,a)||j==-){
i++;
j++;
}
else j=f[j];
if(j==m){
i++;
j=;
num++;
}
}
printf("%d\n",num);
}
int main()
{
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=;i<n;++i){
scanf("%d",&x[i]);
}
for(int i=;i<m;++i){
scanf("%d",&a[i]);
}
if(m==){
printf("%d\n",n);
continue;
}
hand(n-,m-);
solve(n-,m-);
}
return ;
}
HDU 4749-Parade Show(KMP变形)的更多相关文章
- HDU 4749 Parade Show(贪心+kmp)
题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...
- HDU 4749 Parade Show(暴力水果)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 Problem Description 2013 is the 60 anniversary ...
- HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...
- HDU 1686 Oulipo(KMP变形求子串出现数目(可重))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...
- HDU 4749: Parade Show
看大神代码,发现上交大神很棒的一个思路 题意: 在源数字串中找出尽量多的连续子串,要求子串任意两值的大小关系与目标串相同位置的值的大小关系相同.求源串能拿出的子串的最大数量. 关键词: RK-Hash ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU-4749 Parade Show KMP算法 | DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...
- HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 解题报告:输入一篇文章,从头开始,当遇到 “Apple”, “iPhone”, “iPod”, ...
- HDU 5763 Another Meaning KMP+DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- hdu 3441 Rotation
总的来说,这题要2次用到polya定理. 由题目条件A*A=B*B+1,变形为(A-1)*(A+1)=K*B*B; 分别分解A-1和A+1的质因数,在合并在一起. 第一步:搜索B,对B*B的正方形涂色 ...
- C Primer Plus之高级数据表示
抽象数据类型(ADT) 类型是由什么组成?一个类型(type)指定两类信息:一个属性集和一个操作集. 所以您想定义一个新的数据类型.首先,您需要提供存储数据的方式,可能是通过设计一个结构.第二 ...
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- SpingMVC中利用BindingResult将错误信息返回到页面中
SpingMVC中利用BindingResult将错误信息返回到页面中. ActionFrom中: private String name; private String password; get( ...
- Java基础:三步学会Java Socket编程
Java基础:三步学会Java Socket编程 http://tech.163.com 2006-04-10 09:17:18 来源: java-cn 网友评论11 条 论坛 第一步 ...
- iOS 相机和相册使用授权
1.判断用户是否有权限访问相册 授权一次后,不在提示是否授权 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus a ...
- 246. Strobogrammatic Number
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
- C++:运算符重载函数之"++"、"--"、"[ ]"、"=="的应用
5.2.5 "++"和"--"的重载 对于前缀方式++ob,可以用运算符函数重载为: ob.operator++() //成员函数重载 或 operator++ ...
- 【Android多屏适配】动态改变Listview item高度
在ListView的Adapter中去直接获取传入View的LayoutParams是会报空指针异常的,唯一的方法是在xml中嵌套布局一层LinearLayout <?xml version=& ...
- nyoj-291 互素数个数 欧拉函数
LK的数学题 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 LK最近遇到一个问题,需要你帮她一下.一个整数n,求[1,n)中,和n互素的数的个数. 输入 多组测 ...