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

ABC149-D Prediction and Restriction


問題へのリンク


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("5 2");
            WillReturn.Add("8 7 6");
            WillReturn.Add("rsrpr");
            //27
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("7 1");
            WillReturn.Add("100 10 1");
            WillReturn.Add("ssssppr");
            //211
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("30 5");
            WillReturn.Add("325 234 123");
            WillReturn.Add("rspsspspsrpspsppprpsprpssprpsr");
            //4996
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        long K = wkArr[1];

        SplitAct(InputList[1]);

        long R = wkArr[0];
        long S = wkArr[1];
        long P = wkArr[2];

        string T = InputList[2];

        long Answer = 0;
        char[] LastTeArr = new char[K];
        for (int I = 0; I <= T.Length - 1; I++) {
            long ModVal = I % K;

            char CurrTe = T[I];
            char WinTe = '\0';

            // 勝つ手を求める
            if (CurrTe == 'r') WinTe = 'p';
            if (CurrTe == 's') WinTe = 'r';
            if (CurrTe == 'p') WinTe = 's';

            if (LastTeArr[ModVal] == WinTe) {
                LastTeArr[ModVal] = '\0';
                continue;
            }
            LastTeArr[ModVal] = WinTe;

            if (WinTe == 'r') Answer += R;
            if (WinTe == 's') Answer += S;
            if (WinTe == 'p') Answer += P;
        }
        Console.WriteLine(Answer);
    }
}


解説

K手前と同じ手が出せないので
mod K の配列 で K手前の手を配列に記憶しつつ
貪欲法を使ってます。