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

ABC386-C Operate 1


問題へのリンク


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("1");
            WillReturn.Add("abc");
            WillReturn.Add("agc");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1");
            WillReturn.Add("abc");
            WillReturn.Add("awtf");
            //No
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1");
            WillReturn.Add("abc");
            WillReturn.Add("ac");
            //Yes
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("1");
            WillReturn.Add("back");
            WillReturn.Add("black");
            //Yes
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("1");
            WillReturn.Add("same");
            WillReturn.Add("same");
            //Yes
        }
        else if (InputPattern == "Input6") {
            WillReturn.Add("1");
            WillReturn.Add("leap");
            WillReturn.Add("read");
            //No
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static string mS;
    static string mT;

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

        if (mS == mT) {
            Console.WriteLine("Yes");
            return;
        }

        if (IsOK3()) {
            Console.WriteLine("Yes");
            return;
        }
        if (IsOK12()) {
            Console.WriteLine("Yes");
            return;
        }

        Console.WriteLine("No");
    }

    static bool IsOK12()
    {
        if (Math.Abs(mS.Length - mT.Length) > 1) {
            return false;
        }

        var InsLinkedList1 = new LinkedList<char>();
        var InsLinkedList2 = new LinkedList<char>();
        foreach (char EachChar in mS) InsLinkedList1.AddFirst(EachChar);
        foreach (char EachChar in mT) InsLinkedList2.AddFirst(EachChar);

        while (InsLinkedList1.Count > 0 && InsLinkedList2.Count > 0) {
            char FisrtChar1 = InsLinkedList1.First.Value;
            char FisrtChar2 = InsLinkedList2.First.Value;

            if (FisrtChar1 == FisrtChar2) {
                InsLinkedList1.RemoveFirst();
                InsLinkedList2.RemoveFirst();
                continue;
            }
            break;
        }

        while (InsLinkedList1.Count > 0 && InsLinkedList2.Count > 0) {
            char LastChar1 = InsLinkedList1.Last.Value;
            char LastChar2 = InsLinkedList2.Last.Value;

            if (LastChar1 == LastChar2) {
                InsLinkedList1.RemoveLast();
                InsLinkedList2.RemoveLast();
                continue;
            }
            break;
        }

        int Cnt = InsLinkedList1.Count + InsLinkedList2.Count;
        return Cnt <= 1;
    }

    static bool IsOK3()
    {
        if (mS.Length != mT.Length) return false;

        int UnMatchCnt = 0;
        for (int I = 0; I <= mS.Length - 1; I++) {
            if (mS[I] != mT[I]) {
                UnMatchCnt++;
            }
        }
        return UnMatchCnt <= 1;
    }
}


解説

LinkedListで
前から削る処理をシュミレーションしてから
後から削る処理をシュミレーションしてます。