結果

問題 No.1018 suffixsuffixsuffix
ユーザー Kiri8128Kiri8128
提出日時 2020-04-04 01:49:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 392 ms / 2,000 ms
コード長 3,802 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 124,160 KB
最終ジャッジ日時 2024-07-03 06:42:54
合計ジャッジ時間 9,438 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,120 KB
testcase_01 AC 41 ms
53,504 KB
testcase_02 AC 41 ms
53,504 KB
testcase_03 AC 41 ms
53,452 KB
testcase_04 AC 42 ms
52,864 KB
testcase_05 AC 40 ms
53,248 KB
testcase_06 AC 41 ms
53,248 KB
testcase_07 AC 41 ms
53,248 KB
testcase_08 AC 42 ms
53,248 KB
testcase_09 AC 162 ms
88,968 KB
testcase_10 AC 163 ms
89,872 KB
testcase_11 AC 168 ms
91,004 KB
testcase_12 AC 162 ms
89,788 KB
testcase_13 AC 166 ms
90,488 KB
testcase_14 AC 197 ms
95,060 KB
testcase_15 AC 353 ms
117,108 KB
testcase_16 AC 366 ms
118,776 KB
testcase_17 AC 340 ms
115,296 KB
testcase_18 AC 214 ms
99,624 KB
testcase_19 AC 123 ms
84,516 KB
testcase_20 AC 119 ms
84,992 KB
testcase_21 AC 126 ms
84,736 KB
testcase_22 AC 129 ms
84,736 KB
testcase_23 AC 124 ms
84,992 KB
testcase_24 AC 354 ms
115,332 KB
testcase_25 AC 374 ms
120,692 KB
testcase_26 AC 380 ms
124,160 KB
testcase_27 AC 351 ms
117,008 KB
testcase_28 AC 343 ms
116,676 KB
testcase_29 AC 148 ms
96,512 KB
testcase_30 AC 144 ms
94,336 KB
testcase_31 AC 146 ms
92,544 KB
testcase_32 AC 148 ms
97,024 KB
testcase_33 AC 146 ms
95,360 KB
testcase_34 AC 43 ms
52,864 KB
testcase_35 AC 88 ms
89,816 KB
testcase_36 AC 93 ms
89,052 KB
testcase_37 AC 392 ms
122,348 KB
権限があれば一括ダウンロードができます

ソースコード

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:
    print(*[N * M - k for k in K])
elif M <= 2:
    S *= M
    SA = SA_IS(S)[1:]
    print(*[SA[k] + 1 for k in K])
else:
    
    for i in range(1, int(N**0.5)+2):
        if N % i == 0 and S[i:] == S[:-i]:
            S = S[:i]
            M *= N // i
            N = i
            break
    
    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])
    def calc(k):
        t = bl(X, k + 1 << 47) - 1
        a, b = chk(X[t])
        return b - (k - a) * N + 1
    print(*[calc(k) for k in K])
0