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/ ...
随机推荐
- POJ3087Shuffle'm Up(map)
http://poj.org/problem?id=3087 题意 : 我只能说,,英语不好是硬伤...这个题比较别扭啊,不知道真正题意是不是我所想的,我先把我A了的代码按照的题意的意思说一下,就是说 ...
- 将DJANGO管理界面的filter_horizontal移到前面来复用
参考URL: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/reusing-djangos-filter_horizontal ...
- lintcode 中等题:A + B Problem A + B 问题
题目: 中等 A + B 问题 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 如果 a=1 并且 b=2,返回3 注意 你不需要从输入流读入数据,只需要根据aplusb的两个参数 ...
- Java Logger(java日志)
目录 1. 简介2. 安装3. log4j基本概念3.1. Logger3.2. Appender3.2.1. 使用ConsoleAppender3.2.2. 使用FileAppender3.2.3. ...
- Delphi判断字符串是否是数字、字母、大小写字母
function IsNumberic(Vaule:String):Boolean; //判断Vaule是不是数字 var i:integer; begin result:=true; //设置返回值 ...
- [iOS]解决模拟器无法输入中文问题
第一步:设置schem 菜单项 -> Product-> Scheme -> Edit Scheme -> 然后在弹出的界面里 选择OPtion 项, 设置 Applicat ...
- Android The content of the adapter has changed but ListView did not receive a notification
The content of the adapter has changed but ListView did not receive a notification. Make sure the co ...
- 简单说说PHP优化那些事
我们在编写程序时,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行速度与最 ...
- NFC(4)响应NFC设备时启动activity的四重过滤机制
响应NFC设备时启动activity的四重过滤机制 在一个NFC设备读取NFC标签或另一个NFC设备中的数据之前会在0.1秒之内建立NFC连接,然后数据会自动从被读取一端流向读取数据的一端(NFC设备 ...
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...