using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input4"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("3"); WillReturn.Add("veryverygoodproblem"); WillReturn.Add("goodexcellentproblems"); WillReturn.Add("problemgood"); //0 //0 //10 } else if (InputPattern == "Input2") { WillReturn.Add("2"); WillReturn.Add("goodproblem"); WillReturn.Add("agdddekproblemsdl"); //0 //2 } else if (InputPattern == "Input3") { WillReturn.Add("9"); WillReturn.Add("geodaaaproblem"); WillReturn.Add("goodproblen"); WillReturn.Add("badendlessprobrem"); WillReturn.Add("podpdpprrobleem"); WillReturn.Add("problemgoodprobremgood"); WillReturn.Add("smallproblem"); WillReturn.Add("proproprefurohobbyhobby"); WillReturn.Add("gooeproblemd"); WillReturn.Add("itisproblemcoolgoodproblem"); //1 //1 //4 //5 //2 //4 //8 //1 //1 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { var sw = System.Diagnostics.Stopwatch.StartNew(); List InputList = GetInputList(); string[] SArr = InputList.Skip(1).ToArray(); Array.ForEach(SArr, X => DeriveCost(X)); } //GoodProblemに変換する際の最小コストを求める static void DeriveCost(string pBeforeStr) { //problemという文字列のmの添字の配列を作成 DeriveProblemIndMArr(pBeforeStr); //Goodに変換するコストのDict var CostGoodDict = DeriveNeedCostDict(pBeforeStr, "good"); //Problemに変換するコストの配列 var CostProblemDict = DeriveNeedCostDict(pBeforeStr, "problem"); int MinCost = int.MaxValue; foreach (var EachPair1 in CostGoodDict) { foreach (var EachPair2 in CostProblemDict) { if (EachPair1.Value + 3 >= EachPair2.Value) continue; int CurrCost = EachPair1.Key + EachPair2.Key; if (MinCost > CurrCost) MinCost = CurrCost; } } Console.WriteLine(MinCost); } //problemという文字列のmの添字の配列を作成 static int[] ProblemIndMArr; static void DeriveProblemIndMArr(string pBeforeStr) { var wkList = new List(); int StaInd = 0; int wkInd = -1; while ((wkInd = pBeforeStr.IndexOf("problem", StaInd)) > -1) { StaInd = wkInd + "problem".Length; wkList.Add(StaInd - 1); if (StaInd > pBeforeStr.Length - 1) break; } ProblemIndMArr = wkList.ToArray(); } //指定文字列の各Indから指定文字列に変換する際のコストのDictを返す static Dictionary DeriveNeedCostDict(string pBeforeStr, string pNeedStr) { var WillReturnDict = new Dictionary(); for (int I = 0; I <= pBeforeStr.Length - 1; I++) { if (I + pNeedStr.Length > pBeforeStr.Length) break; int Cost = 0; for (int J = 0; J <= pNeedStr.Length - 1; J++) { if (pNeedStr[J] != pBeforeStr[I + J]) Cost++; } //goodなら最左を優先 if (pNeedStr == "good") { //左にあるproblemを破壊するコストがかかる Cost += ProblemIndMArr.Count(X => X < I); if (WillReturnDict.ContainsKey(Cost)) continue; WillReturnDict.Add(Cost, I); } //Problemなら最右を優先 if (pNeedStr == "problem") { WillReturnDict[Cost] = I; } } return WillReturnDict; } }