Letter Wheels-JavierWu

发布于 2020-03-22  10 次阅读


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=0i<leni++)
	{
		for(int j=0j<lenj++)
		{
			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=0i<leni++)
	{
		for(int j=0j<lenj++)
		{
			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
}
届ける言葉を今は育ててる
最后更新于 2020-03-22