[codeforces 1365C] Rotation Matching 寻找偏移量

Codeforces Round #648 (Div. 2)  参与排名人数13231

[codeforces 1365C]    Rotation Matching   寻找偏移量

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1365/problem/C

ProblemLangVerdictTimeMemory
C - Rotation Matching GNU C++17Accepted124 ms1600 KB

样例模拟如下

Input:
5
1 2 3 4 5
2 3 4 5 1
Output:
5

a位置          1 2 3 4 5 6 7 8 9 10
a数值          1 2 3 4 5 1 2 3 4 5
b位置            1 2 3 4 5
b数值            2 3 4 5 1
b相对于a的偏移量   1 1 1 1 1

Input:
5
5 4 3 2 1
1 2 3 4 5
Output:
1


a位置          1 2 3 4 5 6 7 8 9 10
a数值          5 4 3 2 1 5 4 3 2 1
b位置                1 2 3 4 5
b数值                2 3 4 5 1
b相对于a的偏移量       0 2 4 1 3

Input:
4
1 3 2 4
4 2 3 1
Output:
2

a位置          1 2 3 4 5 6 7 8
a数值          1 3 2 4 1 3 2 4
b位置                1 2 3 4
b数值                4 2 3 1
b相对于a的偏移量       0 2 0 2

可以看到,最终求的是同一个偏移量对应的最多雷同数量

AC代码如下

#include <cstdio>
#include <algorithm>
#define maxn 200010
using namespace std;
int a,b,cnt[maxn],pos[maxn],mx;
int main(){
	int n,i,offset;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a),pos[a]=i;
	for(i=1;i<=n;i++){
		scanf("%d",&b);
		offset=pos[b]-i;//偏移量
		if(offset<0)offset+=n;
		cnt[offset]++;
	}
	for(i=0;i<n;i++)mx=max(mx,cnt[i]);
	printf("%d\n",mx);
	return 0;
}

类似题目

Panasonic Programming Contest 2020 E Three Substrings 字串交叠+字串拼接

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值