HDOJ 题目 4749 Parade Show(KMP)

Parade Show

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1440    Accepted Submission(s): 580


Problem Description

  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?
 

Input
Multiple cases, end with EOF.
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);
 

Output
One integer, the maximal amount of well designed queues.
 

Sample Input
  
  
10 5 10 2 4 2 4 2 4 2 4 2 4 1 2 1 2 1
 

Sample Output
  
  
1
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5421  5420  5419  5418  5417 

 题目大意:给出每个同学的高度即上边的文本串,再给你的最佳的视觉角度(即上边的模式串),匹配是文本串和模式串的相邻两个的相等大于或小于

思路:按高度差匹配,唯一有点坑的地方就是注意边界数据,当模式串的长度为1时,输出n

ac代码


#include<stdio.h>
#include<string.h>
int n,m,k,ans;
int a[100010],b[100010];
int next[100010];
int cmp(int a,int b)
{
	if(a*b>0)
		return 1;
	if(a==0&&b==0)
		return 1;
	return 0;
}
void getnext(int *a)
{
	int i,j;
	i=0,j=-1;
	next[0]=-1;
	while(i<m)
	{
		if(j==-1||cmp(a[i],a[j]))
		{
			i++;
			j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
}
void kmp(int *a,int *b)
{
	int i,j;
	i=j=0;
	getnext(b);
	while(i<n-1)
	{
		if(j==-1||cmp(a[i],b[j]))
		{
			i++;
			j++;
		}
		else
			j=next[j];
		if(j==m-1)
		{
			j=0;
			i++;
			ans++;
		}
	}
}
int main()
{
	while(scanf("%d%d%d",&n,&m,&k)!=EOF)
	{
		int i;
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		for(i=0;i<n-1;i++)
			a[i]=a[i+1]-a[i];
		for(i=0;i<m;i++)
			scanf("%d",&b[i]);
		for(i=0;i<m-1;i++)
			b[i]=b[i+1]-b[i];
		ans=0;
		if(m==1)
		{
			printf("%d\n",n);
			continue;
		}
		kmp(a,b);
		printf("%d\n",ans);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值