典型90問    次の典型90問へ    前の典型90問へ

典型90問 006 Smallest Subsequence(★5)


問題へのリンク


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("7 3");
            WillReturn.Add("atcoder");
            //acd
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("14 5");
            WillReturn.Add("kittyonyourlap");
            //inlap
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int K = wkArr[1];

        string S = InputList[1];

        // 添え字のList[文字]なDict
        var IndListDict = new Dictionary<char, List<int>>();

        int UB = S.Length - 1;
        for (int I = 0; I <= UB; I++) {
            char CurrChar = S[I];
            if (IndListDict.ContainsKey(CurrChar) == false) {
                IndListDict[CurrChar] = new List<int>();
            }
            IndListDict[CurrChar].Add(I);
        }

        var InsSparseTable = new SparseTable<char>(S, true);

        int CurrInd = 0;
        var sb = new System.Text.StringBuilder();
        while (sb.Length < K) {
            int NeedLen = K - sb.Length;
            int RangeMax = UB - NeedLen + 1;

            char MinChar = InsSparseTable.Query(CurrInd, RangeMax);
            sb.Append(MinChar);

            int ResultInd = ExecNibunhou_LowerBound(CurrInd, IndListDict[MinChar]);
            CurrInd = IndListDict[MinChar][ResultInd] + 1;
        }
        Console.WriteLine(sb.ToString());
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static int ExecNibunhou_LowerBound(int pVal, List<int> pList)
    {
        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList.Last()) {
            return -1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] >= pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }
}

#region SparseTable
// スパーステーブル
internal class SparseTable<Type>
{
    private Type[] mInitArr;
    private int UB_0;
    private int UB_1;

    // 最小値か最大値[開始Ind,2の指数]なArr
    private Type[,] mMinOrMaxArr;

    // Log2の値[2べきな値] なDict
    static Dictionary<int, int> mLogDict = new Dictionary<int, int>();

    // 最小値を取得するか?
    private bool mIsMin = false;

    // コンストラクタ
    internal SparseTable(IEnumerable<Type> pInitEnum, bool pIsMin)
    {
        mIsMin = pIsMin;

        mInitArr = pInitEnum.ToArray();
        UB_0 = mInitArr.GetUpperBound(0);

        int Sisuu = 0;
        int Beki2 = 1;
        while (true) {
            mLogDict[Beki2] = Sisuu;
            if (Beki2 > mInitArr.Length) {
                break;
            }
            Beki2 *= 2;
            Sisuu++;
        }
        UB_1 = Sisuu;

        mMinOrMaxArr = new Type[UB_0 + 1, UB_1 + 1];

        // 長さ1の分を初期化
        for (int I = 0; I <= UB_0; I++) {
            mMinOrMaxArr[I, 0] = mInitArr[I];
        }

        for (int LoopLength = 2; LoopLength < int.MaxValue; LoopLength *= 2) {
            bool WillBreak = true;
            int HalfRange = LoopLength / 2;
            for (int I = 0; I <= UB_0; I++) {
                int StaInd1 = I;
                int EndInd1 = I + HalfRange - 1;
                int StaInd2 = EndInd1 + 1;
                int EndInd2 = StaInd2 + HalfRange - 1;

                if (EndInd2 > UB_0) break;

                var KouhoList = new List<Type>();
                KouhoList.Add(mMinOrMaxArr[I, mLogDict[HalfRange]]);
                KouhoList.Add(mMinOrMaxArr[StaInd2, mLogDict[HalfRange]]);

                if (mIsMin) {
                    mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Min();
                }
                else {
                    mMinOrMaxArr[I, mLogDict[LoopLength]] = KouhoList.Max();
                }

                WillBreak = false;
            }
            if (WillBreak) break;
        }
    }

    // 閉区間 [L,R]の最小値または最大値を求める
    internal Type Query(int pL, int pR)
    {
        // 区間を内包する2べきを返す
        int Beki2 = 1;
        int Sisuu = 0;
        while (true) {
            int LeftRangeMax = pL + Beki2 - 1;
            int RightRangeMin = pR - Beki2 + 1;

            if (LeftRangeMax + 1 >= RightRangeMin) {
                break;
            }
            Beki2 *= 2;
            Sisuu++;
        }

        var KouhoList = new List<Type>();
        KouhoList.Add(mMinOrMaxArr[pL, Sisuu]);
        KouhoList.Add(mMinOrMaxArr[pR - Beki2 + 1, Sisuu]);

        if (mIsMin) {
            return KouhoList.Min();
        }
        return KouhoList.Max();
    }
}
#endregion


解説

辞書順最小なので、
(残りの文字が不足しない範囲で)貪欲に最小文字を選んでます。