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

ABC477-C Range Search Query


問題へのリンク


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");
            WillReturn.Add("abcdabc");
            WillReturn.Add("bc");
            WillReturn.Add("2 6");
            WillReturn.Add("3 5");
            WillReturn.Add("3 7");
            //Yes
            //No
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("bbbaaabbbaaaaaabbbab");
            WillReturn.Add("bbb");
            WillReturn.Add("5 16");
            WillReturn.Add("5 6");
            WillReturn.Add("2 16");
            WillReturn.Add("6 7");
            WillReturn.Add("17 19");
            WillReturn.Add("3 7");
            WillReturn.Add("7 16");
            WillReturn.Add("7 20");
            WillReturn.Add("17 19");
            WillReturn.Add("2 6");
            //Yes
            //No
            //Yes
            //No
            //No
            //No
            //Yes
            //Yes
            //No
            //No
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S = InputList[1];
        string T = InputList[2];
        int UB = S.Length - 1;

        // OKな開始IndのList
        var OKStaIndList = new List<int>();
        for (int I = 0; I <= UB; I++) {
            bool IsOK = true;
            for (int J = 0; J <= T.Length - 1; J++) {
                int Curr1 = I + J;
                int Curr2 = J;
                if (Curr1 > UB) {
                    IsOK = false;
                    break;
                }
                if (S[Curr1] != T[Curr2]) {
                    IsOK = false;
                    break;
                }
            }
            if (IsOK) {
                OKStaIndList.Add(I);
            }
        }

        var sb = new System.Text.StringBuilder();

        int[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);
        foreach (string EachStr in InputList.Skip(3)) {
            SplitAct(EachStr);
            int RangeSta = wkArr[0] - 1;
            int RangeEnd = wkArr[1] - 1;

            int ResultInd = ExecNibunhou_LowerBound(RangeSta, OKStaIndList);
            if (ResultInd == -1) {
                sb.AppendLine("No");
                continue;
            }
            int CurrInd = OKStaIndList[ResultInd];
            if (CurrInd + T.Length - 1 > RangeEnd) {
                sb.AppendLine("No");
                continue;
            }
            sb.AppendLine("Yes");
        }
        Console.Write(sb.ToString());
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static int ExecNibunhou_LowerBound(int pVal, List<int> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素が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;
    }
}


解説

最初にOKな開始IndのListを用意。
クエリごとに、LowerBoundでOKな開始Indの中での最小Indを求め
クエリの区間内で可能か判定してます。

2026-09-27 コンテストでのメモ
String.IndexOf(対象文字列 , 開始Ind)
で実装を減らしたら、TLE
TLEの原因は、
String.IndexOfだと後続のIndでも調査するので、O( |S| の 2乗になるため)