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

ABC237-D LR insertion


問題へのリンク


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");
            WillReturn.Add("LRRLR");
            //1 2 4 5 3 0
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("7");
            WillReturn.Add("LLLLLLL");
            //7 6 5 4 3 2 1 0
        }
        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>();

        var CurrNode = InsLinkedList.AddLast(0);
        for (int I = 0; I <= S.Length - 1; I++) {
            int CurrVal = I + 1;
            if (S[I] == 'R') {
                CurrNode = InsLinkedList.AddAfter(CurrNode, CurrVal);
            } if (S[I] == 'L') {
                CurrNode = InsLinkedList.AddBefore(CurrNode, CurrVal);
            }
        }

        var AnswerList = new List<int>();
        foreach (int EachInt in InsLinkedList) {
            AnswerList.Add(EachInt);
        }
        string[] AnswerArr = Array.ConvertAll(AnswerList.ToArray(), pX => pX.ToString());
        string Answer = string.Join(" ", AnswerArr);
        Console.WriteLine(Answer);
    }
}


解説

LinkedListクラスを使ってます。