AOJ本の読書メモ
AOJ
次のAOJの問題へ
前のAOJの問題へ
AOJ 0571 JJOOII
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("OJJOOIIOJOI");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("IJJIIJJJ");
//0
}
else if (InputPattern == "Input3") {
WillReturn.Add("JOIJOIJOIJOIJOI");
//1
}
else if (InputPattern == "Input4") {
WillReturn.Add("OOJJJJJJJOOOOIIIII");
//4
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
string S = InputList[0];
List<RunLen.RunLenInfo> RunLenList = RunLen.DeriveRunLenInfoList(S);
var AnswerList = new List<long>();
AnswerList.Add(0);
for (int I = 0; I <= RunLenList.Count - 1; I++) {
int Ind1 = I;
int Ind2 = I + 1;
int Ind3 = I + 2;
if (Ind3 > RunLenList.Count - 1) continue;
if (RunLenList[Ind1].AppearChar != 'J') continue;
if (RunLenList[Ind2].AppearChar != 'O') continue;
if (RunLenList[Ind3].AppearChar != 'I') continue;
long JLen = RunLenList[Ind1].RunLen;
long OLen = RunLenList[Ind2].RunLen;
long ILen = RunLenList[Ind3].RunLen;
if (JLen >= OLen && OLen <= ILen) {
AnswerList.Add(OLen);
}
}
Console.WriteLine(AnswerList.Max());
}
}
#region RunLen
// ランレングス圧縮(文字列)
internal class RunLen
{
// ランレングス圧縮情報
internal struct RunLenInfo
{
internal char AppearChar;
internal int RunLen;
}
// ランレングス圧縮結果を返す
internal static List<RunLenInfo> DeriveRunLenInfoList(string pBaseStr)
{
var WillReturn = new List<RunLenInfo>();
int StrUB = pBaseStr.Length - 1;
char PrevChar = pBaseStr[0];
int StrLen = 0;
for (int I = 0; I <= StrUB; I++) {
if (pBaseStr[I] != PrevChar) {
RunLenInfo WillAdd;
WillAdd.AppearChar = PrevChar;
WillAdd.RunLen = StrLen;
WillReturn.Add(WillAdd);
StrLen = 0;
PrevChar = pBaseStr[I];
}
StrLen++;
if (I == StrUB) {
RunLenInfo WillAdd;
WillAdd.AppearChar = pBaseStr[I];
WillAdd.RunLen = StrLen;
WillReturn.Add(WillAdd);
}
}
return WillReturn;
}
}
#endregion
解説
ランレングス圧縮し、
JOIが連続していれば、解候補になりえます。