AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC113-C String Invasion


問題へのリンク


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("accept");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("atcoder");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("anerroroccurred");
            //16
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[0];
        int UB = S.Length - 1;

        long Answer = 0;
        var CntDict = new Dictionary<char, int>();
        for (int I = UB; 0 <= I; I--) {
            char CurrChar = S[I];
            if (CntDict.ContainsKey(CurrChar) == false) {
                CntDict[CurrChar] = 0;
            }
            CntDict[CurrChar]++;

            int PrevInd = I + 1;
            if (PrevInd > UB) continue;

            char PrevChar = S[PrevInd];
            if (CurrChar != PrevChar) continue;

            Answer += CntDict.Where(pX => pX.Key != CurrChar).Sum(pX => pX.Value);
            CntDict.Clear();
            CntDict[CurrChar] = UB - I + 1;
        }
        Console.WriteLine(Answer);
    }
}


解説

文字列を逆から走査して、同じ文字の連続を検知したら、
変換処理を行ってます。