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
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Rotation Matching | GNU C++17 | Accepted | 124 ms | 1600 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 字串交叠+字串拼接