AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC191-A Replace Digits


問題へのリンク


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("3 3");
            WillReturn.Add("191");
            WillReturn.Add("325");
            //593
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 9");
            WillReturn.Add("191");
            WillReturn.Add("998244353");
            //993
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("11 13");
            WillReturn.Add("31415926535");
            WillReturn.Add("2718281828459");
            //98888976555
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        char[] SArr = InputList[1].ToCharArray();
        char[] TArr = InputList[2].ToCharArray();

        var Que = new Queue<char>(TArr.OrderByDescending(pX => pX));

        long CurrSInd = 0;
        while (Que.Count > 0) {
            char Dequeued = Que.Dequeue();
            while (true) {
                if (SArr[CurrSInd] < Dequeued) {
                    SArr[CurrSInd] = Dequeued;

                    CurrSInd++;
                    if (CurrSInd > SArr.GetUpperBound(0)) {
                        CurrSInd = SArr.GetUpperBound(0);
                    }
                    break;
                }
                if (CurrSInd < SArr.GetUpperBound(0)) {
                    CurrSInd++;
                    continue;
                }
                break;
            }
        }

        // 最後にある捨てることが不可な文字の、使用有無を調べる
        if (Array.IndexOf(SArr, TArr.Last()) >= 0) {
            Console.WriteLine(new string(SArr));
        }
        else {
            SArr[SArr.GetUpperBound(0)] = TArr.Last();
            Console.WriteLine(new string(SArr));
        }
    }
}


解説

文字を捨てることが可能だとして、
Tの降順で、Sの上位桁から値を設定していきます。

最後に、Tの最後の文字の使用有無を見て、
未使用なら末尾の桁で使用するようにしてます。