题意:

给出一个母串和一个模式串求母串中最多能分成最大的子串数(每个字串中的各个数字的大小关系和模式串相同)

分析:

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

  1. HDU 4749 Parade Show(贪心+kmp)

    题目链接 题目都看不懂,做毛线...看懂了之后就是kmp出,所有的匹配区间,然后DP可以写,贪心也可以做把,DP应该需要优化一下,直接贪,也应该对的,经典贪心问题. #include<iostr ...

  2. HDU 4749 Parade Show(暴力水果)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 Problem Description   2013 is the 60 anniversary ...

  3. HDU 4749 Parade Show 2013 ACM/ICPC Asia Regional Nanjing Online

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题目大意:给一个原序列N,再给出一个序列M,问从N中一共可以找出多少个长度为m的序列,序列中的数 ...

  4. HDU 1686 Oulipo(KMP变形求子串出现数目(可重))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...

  5. HDU 4749: Parade Show

    看大神代码,发现上交大神很棒的一个思路 题意: 在源数字串中找出尽量多的连续子串,要求子串任意两值的大小关系与目标串相同位置的值的大小关系相同.求源串能拿出的子串的最大数量. 关键词: RK-Hash ...

  6. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. HDU-4749 Parade Show KMP算法 | DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...

  8. HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 解题报告:输入一篇文章,从头开始,当遇到 “Apple”, “iPhone”, “iPod”, ...

  9. HDU 5763 Another Meaning KMP+DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. POJ1573Robot Motion

    http://poj.org/problem?id=1573 #include<stdio.h> #include<stdlib.h> #include<cstring& ...

  2. Session过期,跳出iframe等框架

    //在你想控制跳转的页面,如login.jsp中的<head>与</head>之间加入以下代码:    if(window != top){        //解决Sessio ...

  3. leetcode:两个数的和||

    两个数的和|| 给定一个排序数组,求出其中两个数的和等于指定target时,这两个数在原始数组中的下标,返回的下标从1开始 解题 原始数组已经是升序的,找出其中两个数的和等于target 定义两个指针 ...

  4. 【nginx网站性能优化篇(1)】gzip压缩与expire浏览器缓存

    gzip压缩 概述 网页在服务器端经过了gzip或者其他格式的压缩后的输出明显减少了content-length字节,当访问过百万时,这些减少的字节就会变为客观的流量给节约下来;从而减轻服务器的压力以 ...

  5. java nio2

    Buffer的基本用法 使用Buffer读写数据一般遵循以下四个步骤: 写入数据到Buffer 调用flip()方法 从Buffer中读取数据 调用clear()方法或者compact()方法 当向b ...

  6. IOS中实现图片点击全屏预览

    //// ViewController.m// XWZoomImageView//// Created by xiao on 15/11/13.// Copyright © 2015年 xiao. A ...

  7. C# 任意类型数据转JSON格式(转)

    HOT SUMMER 每天都是不一样,积极的去感受生活 C# 任意类型数据转JSON格式 /// <summary> /// List转成json /// </summary> ...

  8. Java API —— IO流( FileInputStream & FileOutputStream & BufferedInputStream & BufferedOutputStream )

    1.IO流概述 · IO流用来处理设备之间的数据传输        · 上传文件和下载文件        · Java对数据的操作是通过流的方式 · Java用于操作流的对象都在IO包中   2.IO ...

  9. distinct用法

    distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字段的任何说 ...

  10. CardView官方教程

    Create Cards CardView extends the FrameLayout class and lets you show information inside cards that ...