CodeForces 471D(2020.3.8训练G题)

题目:
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got hold of lots of wooden cubes somewhere. They started making cube towers by placing the cubes one on top of the other. They defined multiple towers standing in a line as a wall. A wall can consist of towers of different heights.

Horace was the first to finish making his wall. He called his wall an elephant. The wall consists of w towers. The bears also finished making their wall but they didn’t give it a name. Their wall consists of n towers. Horace looked at the bears’ tower and wondered: in how many parts of the wall can he “see an elephant”? He can “see an elephant” on a segment of w contiguous towers if the heights of the towers on the segment match as a sequence the heights of the towers in Horace’s wall. In order to see as many elephants as possible, Horace can raise and lower his wall. He even can lower the wall below the ground level (see the pictures to the samples for clarification).

Your task is to count the number of segments where Horace can “see an elephant”.

Input
The first line contains two integers n and w (1 ≤ n, w ≤ 2·105) — the number of towers in the bears’ and the elephant’s walls correspondingly. The second line contains n integers ai (1 ≤ ai ≤ 109) — the heights of the towers in the bears’ wall. The third line contains w integers bi (1 ≤ bi ≤ 109) — the heights of the towers in the elephant’s wall.

Output
Print the number of segments in the bears’ wall where Horace can “see an elephant”.

题意:有两个数字数组,给了两个数组的长度n和m,也提供了两个数组,求数组2可与数组1匹配次数,但这匹配和传统匹配不太一样,比如比较1 2 3 4 5和2 3 4 5 6两个片段时,2 3 4 5 6可同时对每个数字进行自减或是自加操作,于是两个片段成功匹配,思考题意易得,满足这样匹配的两个片段一定是下一个数字相对于前一个数字的增减情况相同,所以对两个数组进行后一个数减前一个数成为一个新的数组(当然长度为x的数组变新后长度为x-1),然后用kmp算法,可得答案

但也要考虑1的特判,一直没想到= =于是一直wa(n和m可能为1的情况)

如下给出ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>

using namespace std;
const int maxl = 200005;
#define ll long long 
int s1[200005],s2[200005];
int s3[200005],s4[200005];
ll p[200005];

int main()
{

	ll ans = 0;
	ll n, m;
	cin >> n >> m;

	for (ll i = 1; i <= n; i++)
	{
		cin>>s2[i];
	}

	for (ll i = 1; i <= m; i++)
	{
		cin>>s1[i];
	}

	if (m == 1)
		ans += n;//特判不要忘*****

	else if (n >= m)
	{
		ll cnt = 1;
		ll num1, num2;

		for (ll i = 2; i <= m; i++)
		{
			s3[cnt]=s1[i] - s1[i-1];//后数减前数成为新的数组
			cnt++;
		}

		num1 = cnt - 1;//新数组长度减一
		cnt = 1;

		for (ll i = 2; i <= n; i++)
		{
			s4[cnt] = s2[i] - s2[i-1];//同上
			cnt++;
		}

		num2 = cnt-1;	   
		p[1] = 0;
		
		for (ll i = 2, j = 0; i <= num1; i++)//求next数组(p数组)
		{	
			while (s3[j+1] != s3[i] && j != 0) 
				j = p[j];
			if (s3[j+1] == s3[i]) 
				j++;
			p[i] = j;
		}
	
		for (ll i = 1, j = 0; i <= num2; i++)//kmp算法求最大匹配数
		{
			if (j+1 > num1)
				j = p[j];
			while (s3[j+1] != s4[i] && j != 0) 
				j = p[j];
			if (s3[j+1] == s4[i]) 
				j++;
			if (j == num1) 
				ans++;
		}

	}
	    
	printf("%d\n",ans);
	   
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列目,其中包括目E. Kolya and Movie Theatre。 根据目描述,E. Kolya and Movie Theatre问要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值