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

ABC328-D Take ABC


問題へのリンク


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("BAABCBCCABCAC");
            //BCAC
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("ABCABC");
            //
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC");
            //AAABBBCCC
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        var InsLinkedList = new LinkedList<char>();
        foreach (char EachChar in S) {
            InsLinkedList.AddLast(EachChar);
            while (true) {
                if (InsLinkedList.Count <= 2) {
                    break;
                }

                char Char3 = InsLinkedList.Last.Value;
                char Char2 = InsLinkedList.Last.Previous.Value;
                char Char1 = InsLinkedList.Last.Previous.Previous.Value;

                if (Char1 == 'A' && Char2 == 'B' && Char3 == 'C') {
                    InsLinkedList.RemoveLast();
                    InsLinkedList.RemoveLast();
                    InsLinkedList.RemoveLast();
                    continue;
                }
                break;
            }
        }

        var sb = new System.Text.StringBuilder();
        foreach (char EachChar in InsLinkedList) {
            sb.Append(EachChar);
        }
        Console.WriteLine(sb.ToString());
    }
}


解説

LinkedListを使ってます。