AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC430-E Shift String
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("5");
WillReturn.Add("1010001");
WillReturn.Add("1000110");
WillReturn.Add("000");
WillReturn.Add("111");
WillReturn.Add("01010");
WillReturn.Add("01010");
WillReturn.Add("0101");
WillReturn.Add("0011");
WillReturn.Add("100001101110000001010110110001");
WillReturn.Add("101100011000011011100000010101");
//2
//-1
//0
//-1
//22
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
static void Main()
{
List<string> InputList = GetInputList();
var sb = new System.Text.StringBuilder();
for (int I = 1; I <= InputList.Count - 1; I += 2) {
int Result = Solve(InputList[I], InputList[I + 1]);
sb.Append(Result);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
static int Solve(string pA, string pB)
{
int Cnt0A = 0;
int Cnt0B = 0;
for (int I = 0; I <= pA.Length - 1; I++) {
if (pA[I] == '0') Cnt0A++;
if (pB[I] == '0') Cnt0B++;
}
if (Cnt0A != Cnt0B) {
return -1;
}
pA += pA;
int Result = pA.IndexOf(pB);
return Result;
}
}
解説
C#のstringのIndexOfメソッドは、最適化されてるので、
間に合います。