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

第6回PAST E 前から3番目


問題へのリンク


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("11");
            WillReturn.Add("LLRLRCDEFBA");
            //1
            //5
            //2
            //ERROR
            //3
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("36");
            WillReturn.Add("RLLDBBDDLCLDFRLRRLRRFLRDRLALLELCAARF");
            //1
            //2
            //ERROR
            //3
            //ERROR
            //ERROR
            //9
            //ERROR
            //17
            //23
            //26
            //20
            //28
            //31
            //29
            //19
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[1];

        var InsLinkedList = new LinkedList<int>();

        Action<LinkedListNode<int>> RemoveAct = (pRemoveNode) =>
        {
            Console.WriteLine(pRemoveNode.Value);
            InsLinkedList.Remove(pRemoveNode);
        };

        for (int I = 0; I <= S.Length - 1; I++) {
            int CurrNum = I + 1;

            if (S[I] == 'L') {
                InsLinkedList.AddFirst(CurrNum);
            }
            if (S[I] == 'R') {
                InsLinkedList.AddLast(CurrNum);
            }
            if (S[I] == 'A') {
                if (InsLinkedList.Count <= 0) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.First);
            }
            if (S[I] == 'B') {
                if (InsLinkedList.Count <= 1) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.First.Next);
            }
            if (S[I] == 'C') {
                if (InsLinkedList.Count <= 2) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.First.Next.Next);
            }
            if (S[I] == 'D') {
                if (InsLinkedList.Count <= 0) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.Last);
            }
            if (S[I] == 'E') {
                if (InsLinkedList.Count <= 1) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.Last.Previous);
            }
            if (S[I] == 'F') {
                if (InsLinkedList.Count <= 2) Console.WriteLine("ERROR");
                else RemoveAct(InsLinkedList.Last.Previous.Previous);
            }
        }
    }
}


解説

LinkedListクラスを使ってます。