Letter Wheels

Letter Wheels

题解思想来源:https://github.com/KCFindstr/icpc-nac-2020-solutions

There are three horizontal wheels of letters stacked one on top of the other, all with the same number of columns. All wheels have one letter, either ‘ A’, ‘ B’ or ‘ C’, in each of its columns on the edge of the wheel. You may rotate the wheels to adjust the positions of the letters. In a single rotation, you can rotate any single wheel to the right or to the left by one column. The wheels are round, of course, so the first column and last column are adjacent.
在这里插入图片描述
You would like to determine whether it is possible to rotate the wheels so that every column has three distinct letters across the three wheels, and if so, determine the minimum number of rotations required.

Input

The input has exactly three lines. Each line has a string s (2≤|s|≤5⋅103) consisting only of upper-case letters ‘A’, ‘B’ or ‘C’, describing the letters of one wheel in their initial positions. All three strings will be of the same length.

Output

Output a single integer, which is the minimum number of rotations required, or −1 if it isn’t possible.

Sample Input 1
ABC
ABC
ABC
Sample Output 1
2

Sample Input 2
ABBBAAAA
BBBCCCBB
CCCCAAAC
Sample Output 2
3

Sample Input 3
AABB
BBCC
ACAC
Sample Output 3
-1

··重点在于计算偏移量,三行轮盘旋转,计算每对行的所有可行偏移量。完成后,尝试第二行和第三行的所有可能的偏移量,并使用预处理后的结果检查是否产生有效的字母轮。在计算最小旋转时,还应考虑旋转第一行。
··其重点在于:
if(s0[j]==s1[(i+j)%len])
flag[1][i]=0;
if(s0[j]==s2[(i+j)%len])
flag[2][i]=0;
if(s1[j]==s2[(i+j)%len])
flag[0][i]=0;
if(flag[0][(j+len-i)%len]&&flag[1][i]&&flag[2][j])
这4句判断上。

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
#include<set>
using namespace std;
#define LL long long
string s0,s1,s2;
int flag[3][5005];
int cnt=2147483647;
void biaoji(int len)
{
	memset(flag,1,sizeof(flag));
	for(int i=0;i<len;i++)
	{
		for(int j=0;j<len;j++)
		{
			if(s0[j]==s1[(i+j)%len])
				flag[1][i]=0;
			if(s0[j]==s2[(i+j)%len])
				flag[2][i]=0;
			if(s1[j]==s2[(i+j)%len])
				flag[0][i]=0;
		}
	}
}
void chaxun(int len)
{
	for(int i=0;i<len;i++)
	{
		for(int j=0;j<len;j++)
		{
			if(flag[0][(j+len-i)%len]&&flag[1][i]&&flag[2][j])
			{
				cnt = min(cnt, max(i,j));
				cnt=min(cnt,max(len-i,len-j));
				cnt = min(cnt, min(i,len-i) + min(j,len-j));
			}
		}
	}
}
int main()
{
	cin>>s0;
	cin>>s1;
	cin>>s2;
	int len=s1.length();
	biaoji(len);
	chaxun(len);
	if(cnt==2147483647)
		cout<<-1<<endl;
	else
		cout<<cnt<<endl;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值