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

ABC098-C Attention


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5");
            WillReturn.Add("WEEWW");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("12");
            WillReturn.Add("WEWEWEEEWWWE");
            //4
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8");
            WillReturn.Add("WWWWWEEE");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        var WestRuisekiDict = new Dictionary<int, int>();
        int WestRuiseki = 0;
        for (int I = 0; I <= UB; I++) {
            WestRuisekiDict[I] = WestRuiseki;
            if (S[I] == 'W') WestRuiseki++;
        }

        var EastRuisekiDict = new Dictionary<int, int>();
        int EastRuiseki = 0;
        for (int I = UB; 0 <= I; I--) {
            EastRuisekiDict[I] = EastRuiseki;
            if (S[I] == 'E') EastRuiseki++;
        }

        int Answer = int.MaxValue;
        for (int I = 0; I <= S.Length - 1; I++) {
            int CurrCost = WestRuisekiDict[I] + EastRuisekiDict[I];
            if (Answer > CurrCost)
                Answer = CurrCost;
        }
        Console.WriteLine(Answer);
    }
}


解説

Westが西で、Eastが東で
配列の正方向でのWestの累積和と
配列の逆方向でのEastの累積和を求めておいて、

リーダーの位置ごとに、向く方向を変える人数を求めてます。