HDU 4749 Parade Show Kmp

24 篇文章 0 订阅
7 篇文章 0 订阅
  这题用的是kmp算法,题意是给出两个队列,每个队列的人的高度,让你求队列二相对于队列一高低趋势匹配的数目总共有多少。高低趋势匹配就是2 4 2 4 2;1 2 1 2 1;
2 比 4 小;1 比 2 小,这就匹配了,一整个队列都符合,算一个。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define maxn 100000
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;

char ch1[maxn + 5], ch2[maxn + 5];
int next1[maxn + 5], n, m, ans;

void get_next(int len)
{
	int i, j = 0;
	next1[1] = 0;
	for(i = 2;i <= len;i++)
	{
		if(ch2[j + 1] != ch2[i]&&j > 0)
		j = next1[j];
		if(ch2[j + 1] == ch2[i])
		j++;
		next1[i] = j;
	}
	return;
}

bool juge(int a, int b)
{
    if(ch1[a] > ch1[a - 1]&&ch2[b] > ch2[b - 1])
	return true;	
	if(ch1[a] < ch1[a - 1]&&ch2[b] < ch2[b - 1])
	return true;
	if(ch1[a] == ch1[a - 1]&&ch2[b] == ch2[b - 1])
	return true;
	return false;
}

void get_ans(int len)
{
	int i, j = 1;
	for(i = 1;i < len;i++)
	{
		if(juge(i + 1, j + 1))
		j++;
		else if(!juge(i + 1, j + 1))
		{
			j = next1[j] + 1;
		}
		if(j == m)
		{
			ans++;
			j = 1;
			i++;
		}
	}
}

int main(int argc, char *argv[])
{
	int k, i, a;
	while(scanf("%d%d%d", &n, &m, &k) != EOF)
	{
		mem(ch1);
		mem(ch2);
		mem(next1);
		ans = 0;
		for(i = 1; i <= n;i++)
		{
			scanf("%d", &a);
			ch1[i] = a + 48;
		}
		for(i = 1;i <= m;i++)
		{
			scanf("%d", &a);
			ch2[i] = a + 48;
		}
		if(m == 1)
		{
		    printf("%d\n", n);
		    continue;
		}
		get_next(m);
		get_ans(n);
		printf("%d\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值