AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC320-C Slot Strategy 2 (Easy)
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("10");
WillReturn.Add("1937458062");
WillReturn.Add("8124690357");
WillReturn.Add("2385760149");
//6
}
else if (InputPattern == "Input2") {
WillReturn.Add("20");
WillReturn.Add("01234567890123456789");
WillReturn.Add("01234567890123456789");
WillReturn.Add("01234567890123456789");
//20
}
else if (InputPattern == "Input3") {
WillReturn.Add("5");
WillReturn.Add("11111");
WillReturn.Add("22222");
WillReturn.Add("33333");
//-1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static string mS1;
static string mS2;
static string mS3;
static int UB;
static List<int> mAnswerKouho = new List<int>();
static void Main()
{
List<string> InputList = GetInputList();
mS1 = InputList[1];
mS2 = InputList[2];
mS3 = InputList[3];
UB = mS1.Length - 1;
for (int I = 0; I <= UB; I++) {
for (int J = 0; J <= UB; J++) {
for (int K = 0; K <= UB; K++) {
AddKouho(I, J, K);
}
}
}
if (mAnswerKouho.Count == 0) {
Console.WriteLine(-1);
}
else {
Console.WriteLine(mAnswerKouho.Min());
}
}
static void AddKouho(int pInd1, int pInd2, int pInd3)
{
var ValSet = new HashSet<char>();
ValSet.Add(mS1[pInd1]);
ValSet.Add(mS2[pInd2]);
ValSet.Add(mS3[pInd3]);
if (ValSet.Count == 1) {
// 止める位置の重複の対応
if (pInd1 == pInd2) {
pInd2 += mS1.Length;
}
if (pInd1 == pInd3) {
pInd3 += mS1.Length;
}
if (pInd2 == pInd3) {
pInd3 += mS1.Length;
}
int Answer = pInd1;
Answer = Math.Max(Answer, pInd2);
Answer = Math.Max(Answer, pInd3);
mAnswerKouho.Add(Answer);
}
}
}
解説
3つの文字列の止める位置のトリオを全て試してます。
止める位置が重複したら、1周待つことに注意が必要です。