AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第2回PAST G ストリング・クエリ


問題へのリンク


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("6");
            WillReturn.Add("1 a 5");
            WillReturn.Add("2 3");
            WillReturn.Add("1 t 8");
            WillReturn.Add("1 c 10");
            WillReturn.Add("2 21");
            WillReturn.Add("2 4");
            //9
            //168
            //0
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("1 x 5");
            WillReturn.Add("1 y 8");
            WillReturn.Add("2 7");
            WillReturn.Add("1 z 8");
            //29
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3");
            WillReturn.Add("1 p 3");
            WillReturn.Add("1 q 100000");
            WillReturn.Add("2 100000");
            //9999400018
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    class RunLenInfoDef
    {
        internal string Str;
        internal long Cnt;
    }

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

        var InsLinkedList = new LinkedList<RunLenInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            string[] SplitArr = EachStr.Split(' ');

            if (SplitArr[0] == "1") {
                RunLenInfoDef AddRunLenInfo = new RunLenInfoDef();
                AddRunLenInfo.Str = SplitArr[1];
                AddRunLenInfo.Cnt = long.Parse(SplitArr[2]);
                InsLinkedList.AddLast(AddRunLenInfo);
            }
            if (SplitArr[0] == "2") {
                var DeleteCntDict = new Dictionary<string, long>();
                long RestCnt = long.Parse(SplitArr[1]);

                while (RestCnt > 0 && InsLinkedList.Count > 0) {
                    RunLenInfoDef FirstRunLenInfo = InsLinkedList.First.Value;
                    long DeleteCnt = Math.Min(RestCnt, FirstRunLenInfo.Cnt);
                    FirstRunLenInfo.Cnt -= DeleteCnt;
                    RestCnt -= DeleteCnt;
                    if (DeleteCntDict.ContainsKey(FirstRunLenInfo.Str) == false) {
                        DeleteCntDict[FirstRunLenInfo.Str] = 0;
                    }
                    DeleteCntDict[FirstRunLenInfo.Str] += DeleteCnt;
                    if (FirstRunLenInfo.Cnt == 0) {
                        InsLinkedList.RemoveFirst();
                    }
                }
                long HeihouSum = 0;
                foreach (long EachLong in DeleteCntDict.Values) {
                    HeihouSum += EachLong * EachLong;
                }
                Console.WriteLine(HeihouSum);
            }
        }
    }
}


解説

ランレングス圧縮した文字列情報を
LinkedListで管理してます。