結果

問題 No.1018 suffixsuffixsuffix
ユーザー Kiri8128Kiri8128
提出日時 2020-04-03 23:20:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,657 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 127,460 KB
最終ジャッジ日時 2023-09-16 04:54:17
合計ジャッジ時間 11,275 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 74 ms
71,300 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left as bl
from bisect import bisect_right as br

def SA_IS(a):
    a += [0]
    k = max(a) + 1
    n = len(a)
    
    def induce_l(sa, a, n, k, stype):
        bucket = get_buckets(a, k, 1)
        for i in range(n):
            j = sa[i] - 1
            if j >= 0 and (not stype[j]):
                sa[bucket[a[j]]] = j
                bucket[a[j]] += 1
    
    def induce_s(sa, a, n, k, stype):
        bucket = get_buckets(a, k, 0)
        for i in range(n)[::-1]:
            j = sa[i] - 1
            if j >= 0 and stype[j]:
                bucket[a[j]] -= 1
                sa[bucket[a[j]]] = j
    
    def get_buckets(a, k, start = 0):
        bucket = [0] * k
        for item in a:
            bucket[item] += 1
        s = 0
        for i in range(k):
            s += bucket[i]
            bucket[i] = s - (bucket[i] if start else 0)
        return bucket
    
    def set_lms(a, n, k, default_order):
        bucket = get_buckets(a, k)
        sa = [-1] * n
        for i in default_order[::-1]:
            bucket[a[i]] -= 1
            sa[bucket[a[i]]] = i
        return sa
    
    def induce(a, n, k, stype, default_order):
        sa = set_lms(a, n, k, default_order)
        induce_l(sa, a, n, k, stype)
        induce_s(sa, a, n, k, stype)
        return sa
    
    def rename_LMS_substring(sa, a, n, stype, LMS, l):
        sa = [_s for _s in sa if LMS[_s]]
        tmp = [-1] * (n//2) + [0]
        dupl = 0
        for _ in range(1, l):
            i, j = sa[_-1], sa[_]
            for ii in range(n):
                if a[i+ii] != a[j+ii] or stype[i+ii] != stype[j+ii]:
                    break
                if ii and (LMS[i+ii] or LMS[j+ii]):
                    dupl += 1
                    break
            tmp[j//2] = _ - dupl
        tmp = [t for t in tmp if t >= 0]
        return tmp, dupl
    
    def calc(a, n, k):
        stype = [1] * n
        for i in range(n-1)[::-1]:
            if a[i] > a[i+1] or (a[i] == a[i+1] and stype[i+1] == 0):
                stype[i] = 0
        
        LMS = [1 if stype[i] and not stype[i-1] else 0 for i in range(n-1)] + [1]
        l = sum(LMS)
        lms = [i for i in range(n) if LMS[i]]
        sa = induce(a, n, k, stype, lms)
        renamed_LMS, dupl = rename_LMS_substring(sa, a, n, stype, LMS, l)
        
        if dupl:
            sub_sa = calc(renamed_LMS, l, l - dupl)
        else:
            sub_sa = [0] * l
            for i in range(l):
                sub_sa[renamed_LMS[i]] = i
        
        lms = [lms[sub_sa[i]] for i in range(l)]
        sa = induce(a, n, k, stype, lms)
        return sa
    
    sa = calc(a, n, k)
    return sa


N, M, Q = map(int, input().split())
S = [ord(a)-96 for a in input()]
K = [int(a)-1 for a in input().split()]

if S.count(S[0]) == N:
    for k in K:
        print(N * M + 1 - k)
elif M <= 2:
    S *= M
    SA = SA_IS(S)[1:]
    for k in K:
        print(SA[k] + 1)
else:
    S *= 2
    SA = SA_IS(S)[1:]

    X = []
    i = 0
    for s in SA:
        if s >= N:
            # X.append((i, (M-1) * N + (s - N)))
            X.append((i << 47) + (M-1) * N + (s - N) + 1)
            i += 1
        else:
            # X.append((i, s))
            X.append((i << 47) + (M-2) * N + s + 1)
            i += M-1
    
    def chk(a):
        return (a >> 47, a - (a >> 47 << 47) - 1)
    
    # print("X =", [chk(x) for x in X])
    X = sorted(X)
    # print("SA =", SA)
    # print("X =", X)
    # print("X =", [chk(x) for x in X])
    for k in K:
        t = bl(X, k + 1 << 47) - 1
        a, b = chk(X[t])
        # print("k, t, a, b =", k, t, a, b)
        print(b - (k - a) * N + 1)

0