AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC372-C Count ABC Again


問題へのリンク


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("7 4");
            WillReturn.Add("ABCDABC");
            WillReturn.Add("4 B");
            WillReturn.Add("3 A");
            WillReturn.Add("5 C");
            WillReturn.Add("4 G");
            //2
            //1
            //1
            //0
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("ABC");
            WillReturn.Add("1 A");
            WillReturn.Add("2 B");
            WillReturn.Add("3 C");
            //1
            //1
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("15 10");
            WillReturn.Add("BBCCBCACCBACACA");
            WillReturn.Add("9 C");
            WillReturn.Add("11 B");
            WillReturn.Add("5 B");
            WillReturn.Add("11 B");
            WillReturn.Add("4 A");
            WillReturn.Add("8 C");
            WillReturn.Add("8 B");
            WillReturn.Add("5 B");
            WillReturn.Add("7 B");
            WillReturn.Add("14 B");
            //0
            //0
            //0
            //0
            //1
            //1
            //2
            //2
            //1
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static char[] mCharArr;
    static int UB;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mCharArr = InputList[1].ToCharArray();
        UB = mCharArr.GetUpperBound(0);

        // DefaultCntを求める
        int CurrCnt = 0;
        for (int I = 0; I <= UB; I++) {
            if (IsABC(I)) CurrCnt++;
        }

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(2)) {
            string[] SplitArr = EachStr.Split(' ');
            int ChangeInd = int.Parse(SplitArr[0]) - 1;
            string AfterStr = SplitArr[1];

            int MinusCnt = 0;
            if (IsABC(ChangeInd - 2)) MinusCnt++;
            if (IsABC(ChangeInd - 1)) MinusCnt++;
            if (IsABC(ChangeInd)) MinusCnt++;

            mCharArr[ChangeInd] = AfterStr[0];

            int PlusCnt = 0;
            if (IsABC(ChangeInd - 2)) PlusCnt++;
            if (IsABC(ChangeInd - 1)) PlusCnt++;
            if (IsABC(ChangeInd)) PlusCnt++;

            CurrCnt -= MinusCnt;
            CurrCnt += PlusCnt;

            sb.Append(CurrCnt);
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

    // 開始Indを引数として、ABCかを返す
    static bool IsABC(int pStaInd)
    {
        int Ind0 = pStaInd;
        int Ind1 = pStaInd + 1;
        int Ind2 = pStaInd + 2;

        if (Ind0 < 0 || UB < Ind0 || mCharArr[Ind0] != 'A') return false;
        if (Ind1 < 0 || UB < Ind1 || mCharArr[Ind1] != 'B') return false;
        if (Ind2 < 0 || UB < Ind2 || mCharArr[Ind2] != 'C') return false;
        return true;
    }
}


解説

変更Indの
-2始まりのABC
-1始まりのABC
-0始まりのABC
の3つを再集計してます。