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算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
随机推荐
- a标签点击事件
onclick="detail(this,'${vo.id}')" function detail(obj,id){ var lb = $("#lb").val ...
- datagrid 溢出文本显示省略号
.datagrid-cell, .datagrid-cell-group, .datagrid-header-rownumber, .datagrid-cell-rownumber{ -o-text- ...
- 在HTML5中如何提高网站前端性能
1. 用web storage替换cookiesCookie最大的问题是每次都会跟在请求后面.在HTML5中,用sessionStorage和localStorage把用户数据直接在客户端,这样 ...
- char类型能否存储一个中文字符?为什么
char类型能否存储一个中文字符?为什么 解答:可以.一个char是两个字节,而一个中文也是两个字节.
- RedHat Ent 6.5 64bit编译安装hadoop2.4.1
RedHat Ent 6.5 64bit编译安装hadoop2.4.1 感谢原帖:http://blog.csdn.net/w13770269691/article/details/16883663/ ...
- 从零开始开发一个vue组件打包并发布到npm (把vue组件打包成一个可以直接引用的js文件)
自己写的组件 有的也挺好的,为了方便以后用自己再用或者给别人用,把组件打包发布到npm是最好不过了,本次打包支持 支持正常的组件调用方式,也支持Vue.use, 也可以直接引用打包好的js文件, 配合 ...
- Ubuntu安装qBittorrent
qBitTorrent是Ubuntu Linux中最受欢迎的P2P软件之中的一个. 出自一名法国大学生之手的qBitTorrent功能强大.界面精美.操作直观. qBitTorrent是Linux中最 ...
- rabbitmq 3.6.11 centos 7 安装
http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm http://www.rabbitmq.co ...
- angular-ui-bootstrap 日历控件国际化
angularjs-angular-ui-bootstrap-changing-language http://stackoverflow.com/questions/19671887/angular ...
- WebService之XFire和SOAP实例(基于JAVA)
开发环境:jdk1.6 + Tomcat7 + MyEclipse10 源码下载地址张贴在文章最后面:首先是使用WSDL协议实现:这里使用XFire XFire一个免费.开源的SOAP框架,它构建了P ...