using System; using System.Collections.Generic; using System.Linq; // https://yukicoder.me/problems/no/252 class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("3"); WillReturn.Add("veryverygoodproblem"); WillReturn.Add("goodexcellentproblems"); WillReturn.Add("problemgood"); } else if (InputPattern == "Input2") { WillReturn.Add("2"); WillReturn.Add("goodproblem"); WillReturn.Add("agdddekproblemsdl"); } 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"); } else if (InputPattern == "Input4") { return System.IO.File.ReadAllLines(@"C:\CSharp\TestConsole\yukicoder_test.txt").ToList(); } 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 string mS; static char[] mCharArr; static int UB_S; static void Main() { List InputList = GetInputList(); var sb = new System.Text.StringBuilder(); foreach (string EachStr in InputList.Skip(1)) { mS = EachStr; mCharArr = EachStr.ToCharArray(); UB_S = mCharArr.GetUpperBound(0); long Answer = Solve(); sb.Append(Answer); sb.AppendLine(); } Console.Write(sb.ToString()); } static int Solve() { Dictionary CostDictGood = DeriveCost("good"); Dictionary CostDictProb = DeriveCost("problem"); // 累積Min作成 int[] ValuesArr = CostDictProb.Values.ToArray(); int UB_ValuesArr = ValuesArr.GetUpperBound(0); var RunMinDict = new Dictionary(); for (int I = ValuesArr.GetUpperBound(0); 0 <= I; I--) { RunMinDict[I] = ValuesArr[I]; if (RunMinDict.ContainsKey(I + 1)) { RunMinDict[I] = Math.Min(RunMinDict[I], RunMinDict[I + 1]); } } HashSet PosSet = GetPosSet(); int Answer = int.MaxValue; int BaseCnt = 0; // goodにする位置を全探索 for (int I = 0; I <= UB_S; I++) { int Cost = BaseCnt; int RangeSta = I + 4; int RangeEnd = UB_ValuesArr; if (RangeSta <= RangeEnd) { Cost += CostDictGood[I] + RunMinDict[RangeSta]; Answer = Math.Min(Answer, Cost); } if (PosSet.Contains(I)) { BaseCnt++; } } return Answer; } // 文字列を引数とし、コスト[開始Ind]なDictを返す static Dictionary DeriveCost(string pTarget) { var CostDict = new Dictionary(); for (int I = 0; I <= UB_S; I++) { if (I + pTarget.Length - 1 > UB_S) break; int Cost = 0; for (int J = 0; J <= pTarget.Length - 1; J++) { if (mCharArr[I + J] != pTarget[J]) { Cost++; } } CostDict[I] = Cost; } return CostDict; } // problemのmの位置のSetを返す static HashSet GetPosSet() { var PosList = new HashSet(); for (int I = 0; I <= UB_S; I++) { if (I + "problem".Length - 1 > UB_S) break; string SubStr = mS.Substring(I, "problem".Length); if (SubStr == "problem") { PosList.Add(I + "problem".Length - 1); } } return PosList; } }