hdu4749 kmp应用
呃,从网上看的题解,然而其实有点地方还没搞懂,先放在这,以后再回来理解。
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4749
题目:2013 is the 60 anniversary of Nanjing University of Science and Technology, and today happens to be the anniversary date. On this happy festival, school authority hopes that the new students to be trained for the parade show. You should plan a better solution to arrange the students by choosing some queues from them preparing the parade show. (one student only in one queue or not be chosen)
Every student has its own number, from 1 to n. (1<=n<=10^5), and they are standing from 1 to n in the increasing order the same with their number order. According to requirement of school authority, every queue is consisted of exactly m students. Because students who stand adjacent in training are assigned consecutive number, for better arrangement, you will choose in students with in consecutive numbers. When you choose these m students, you will rearrange their numbers from 1 to m, in the same order with their initial one.
If we divide our students’ heights into k (1<=k<=25) level, experience says that there will exist an best viewing module, represented by an array a[]. a[i] (1<=i<=m)stands for the student’s height with number i. In fact, inside a queue, for every number pair i, j (1<=i,j<=m), if the relative bigger or smaller or equal to relationship between the height of student number i and the height of student number j is the same with that between a[i] and a[j], then the queue is well designed. Given n students’ height array x[] (1<=x[i]<=k), and the best viewing module array a[], how many well designed queues can we make at most?
First line, 3 integers, n (1<=n<=10^5) m (1<=m<=n) k(1<=k<=25),
Second line, n students’ height array x[] (1<=x[i]<=k,1<=i<=n);
Third line, m integers, best viewing module array a[] (1<=a[i]<=k,1<=i<=m);
2 4 2 4 2 4 2 4 2 4
1 2 1 2 1
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos((double)-1)
#define E exp(double(1))
#define K 100000+9
int g1[][K],g2[][K];
int a[K],b[K],n,m,k;
int nt[K];
int check(int x,int y)
{
int c1,c2,c3;
c1=c2=c3=;
if(g2[b[x]][x-]!=g2[b[y]][y-]-g2[b[y]][y-x])return ;
for(int i=;i<b[x];i++)c1+=g2[i][x-];
for(int i=;i<b[y];i++)c2+=g2[i][y-];
for(int i=;i<b[y];i++)c3+=g2[i][y-x];
return c1==c2-c3;
} void kmp_next(void)
{
memset(nt,,sizeof(nt));
for(int i=,j=;i<=m;i++)
{
while(j && !check(j+,i))
j=nt[j];
if(check(j+,i))j++;
nt[i]=j;
}
}
bool ok(int x,int y)
{
int c1,c2,c3;
c1=c2=c3=;
if(g2[b[x]][x-]!=g1[a[y]][y-]-g1[a[y]][y-x])return ;
for(int i=;i<b[x];i++)c1+=g2[i][x-];
for(int i=;i<a[y];i++)c2+=g1[i][y-];
for(int i=;i<a[y];i++)c3+=g1[i][y-x];
return c1==c2-c3;
}
int kmp(void )
{
int ans=;
for(int i=,j=;i<=n;i++)
{
while(j&&!ok(j+,i))
j=nt[j];
if(ok(j+,i))
j++;
if(j==m)
ans++,j=;
}
return ans;
}
int main(void)
{
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(g1,,sizeof(g1));
memset(g2,,sizeof(g2));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
for(int j=;j<=k;j++)g1[j][i]=g1[j][i-];
g1[a[i]][i]++;
}
for(int i=;i<=m;i++)
{
scanf("%d",&b[i]);
for(int j=;j<=k;j++)g2[j][i]=g2[j][i-];
g2[b[i]][i]++;
}
kmp_next();
printf("%d\n",kmp());
} return ;
}
hdu4749 kmp应用的更多相关文章
- hdu4749 kmp改进
这题说的是给了一个模板串 然后又给了一个串 需要找出类似的按个模板串 , 改相等的位置要相等 该大于的位置到大于 我们将模板串做好失配指针就ok了,然后匹配和原来的匹配不同,这个匹配需要的是相对匹配, ...
- HDU4749 Parade Show(KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给出两个数字串A.B.问A中有多少不相交的子串a能匹配B.匹配的意思是a中任意两个位置i和 ...
- HDU-4749 Parade Show KMP算法 | DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4749 题意:给两个串S和P,求S串中存在多少个与P串的大小关系一样的串. 因为数字的范围是1<= ...
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
随机推荐
- CVPR(IEEE Conference on Computer Vision and Pattern Recognition)
论文提交时间:11月份中旬左右会议时间:7月份左右 CVPR 2017: 网址:http://cvpr2017.thecvf.com/ 接受论文数:782
- 第二百一十五节,jQuery EasyUI,DateBox(日期输入框)组件
jQuery EasyUI,DateBox(日期输入框)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 DateBox(日期输入框)组件的使 ...
- 在项目中用过Spring的哪些方面?及用过哪些Ajax框架?
在项目中用过Spring的哪些方面?及用过哪些Ajax框架? 解答:在项目使用过Spring IOC ,AOP,DAO,ORM,还有上下文环境. 在项目使用过Ext,Juery等Ajax框架.
- Engineer in the White Spaces
 Engineer in the White Spaces Michael Nygard A SySTEM ConSiSTS oF inTERdEpEndEnT pRogRAMS. We call ...
- 微信 Mustache
最近微信小程序非常火,对于前端开发的程序员是个利好的消息,这里主要记录下微信小程序 Mustache语法. 小程序开发的wxml里,用到了Mustache语法.所以,非常有必要把Mustache研究 ...
- 算法之去掉vetor集合中的重复元素
public class DropRepetition { public static void main(String[] args) { Vector<String> v = new ...
- centos6.5下安装samba服务器与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html http://www.cnblogs.com/x_wukong/p/56 ...
- jQuery Validation Engine 表单验证,自定义规则验证方法
jQuery Validation Engine 表单验证说明文档http://code.ciaoca.com/jquery/validation-engine/ js加到jquery.validat ...
- 《JavaScript高级程序设计》读书笔记1--HTTP头部信息
优秀的书籍常读常新 ----------------------------------------- 在发送XHR请求的同时,还会发送如下头部信息: --Accept: 浏览器能够处理的内容类型 - ...
- float元素一定要闭合
float:left; float:right; 一定要两个元素一起放float