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/ ...
随机推荐
- 对cost函数的概率解释
Likehood函数即似然函数,是概率统计中经常用到的一种函数,其原理网上很容易找到,这里就不讲了.这篇博文主要讲解Likelihood对回归模型的Probabilistic interpretati ...
- [hackerrank]Palindrome Index
简单题. #include <iostream> #include <string> using namespace std; int main() { int T; cin ...
- 【mysql的设计与优化专题(6)】mysql索引攻略
所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找,而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...
- AsyncTask 与 Thread+Handler
AsyncTask是封装好的线程池,比起Thread+Handler的方式,AsyncTask在操作UI线程上更方便,因为onPreExecute().onPostExecute()及更新UI方法on ...
- Linux autoconf和automake使用
作为Linux下的程序开发人员,一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Mak ...
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...
- Java API —— 泛型
1.泛型概述及使用 JDK1.5以后出现的机制 泛型是一种特殊的类型,它把指定类型的工作推迟到客户端代码声明并实例化类或方法的时候进行.也被称为参数化类型,可以把类型当作参数一样传递过来,在传递过来之 ...
- OpenCV码源笔记——RandomTrees (一)
OpenCV2.3中Random Trees(R.T.)的继承结构: API: CvRTParams 定义R.T.训练用参数,CvDTreeParams的扩展子类,但并不用到CvDTreeParams ...
- C#实现Comparable接口实现排序
C#中,实现排序的方法有两种,即实现Comparable或Comparer接口,下面简单介绍实现Comparable接口实现排序功能. 实现Comparable接口需要实现CompareTo(obje ...
- python学习,dict的映射练习
练习dict的映射 #coding:utf-8 #问题: a->c, b->d, c->e... 现在有结果字符串求原字符串 dict1={'a':'c', 'b':'d', 'c' ...