using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("5"); WillReturn.Add("WWWWWWWWWGR"); WillReturn.Add("WWWWGWGRR"); WillReturn.Add("WWWWWWWWWWWWWWGRRG"); WillReturn.Add("WGRWGR"); WillReturn.Add("WWWWWWWWWWWWWWWWWGGWGWGGWGWGGGWGG"); //possible //possible //impossible //possible //impossible //1つ目:1つの系統のバスしかありません //2つ目:2つの系統のバスがあります //3つ目:片方の系統において, // 終バスの後に緑のライトのバスが走っている感じでなんかおかしいです. //4つ目:1つの系統の終バスが終わってから, // もう一方の系統が走り始めるなんてこともあるんですね. //5つ目:終バスがないなんてことはない } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); foreach (string EachStr in InputList.Skip(1)) { if (IsPossible(EachStr)) Console.WriteLine("possible"); else Console.WriteLine("impossible"); } } static bool IsPossible(string pStr) { //正規表現RBW+を受信するオートマンの、現在位置ごとの件数 int RCnt = 0; int GCnt = 0; int WCnt = 0; foreach (char CurrChar in pStr.Reverse()) { if (CurrChar == 'R') { RCnt++; } else if (CurrChar == 'G') { if (RCnt == 0) return false; RCnt--; GCnt++; } else if (CurrChar == 'W') { if (GCnt > 0) { GCnt--; WCnt++; } else if (WCnt == 0) return false; } } if (RCnt > 0) return false; if (GCnt > 0) return false; return true; } }