結果

問題 No.2454 Former < Latter
ユーザー tassei903tassei903
提出日時 2023-09-01 23:32:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,039 ms / 2,000 ms
コード長 4,282 bytes
コンパイル時間 500 ms
コンパイル使用メモリ 87,060 KB
実行使用メモリ 127,564 KB
最終ジャッジ日時 2023-09-01 23:32:44
合計ジャッジ時間 8,349 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,408 KB
testcase_01 AC 75 ms
71,584 KB
testcase_02 AC 182 ms
107,336 KB
testcase_03 AC 165 ms
114,748 KB
testcase_04 AC 267 ms
122,036 KB
testcase_05 AC 248 ms
110,256 KB
testcase_06 AC 278 ms
118,792 KB
testcase_07 AC 239 ms
115,540 KB
testcase_08 AC 373 ms
80,176 KB
testcase_09 AC 281 ms
98,548 KB
testcase_10 AC 278 ms
101,524 KB
testcase_11 AC 166 ms
114,888 KB
testcase_12 AC 168 ms
114,892 KB
testcase_13 AC 171 ms
126,744 KB
testcase_14 AC 169 ms
126,632 KB
testcase_15 AC 338 ms
114,776 KB
testcase_16 AC 310 ms
109,324 KB
testcase_17 AC 1,039 ms
94,632 KB
testcase_18 AC 378 ms
81,472 KB
testcase_19 AC 324 ms
127,564 KB
testcase_20 AC 322 ms
112,968 KB
testcase_21 AC 362 ms
80,256 KB
testcase_22 AC 281 ms
97,040 KB
testcase_23 AC 286 ms
98,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################
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

# Longest Common Prefix
# (文字列s, 文字列長n, Suffix Array)を引数として与える
def LCP(s, n, sa):
    lcp = [-1]*(n+1)
    rank = [0]*(n+1)
    for i in range(n+1): rank[sa[i]] = i

    h = 0
    lcp[0] = 0
    for i in range(n):
        j = sa[rank[i] - 1]
        if h > 0: h -= 1
        while j+h < n and i+h < n and s[j+h]==s[i+h]:
            h += 1
        lcp[rank[i] - 1] = h
    return lcp
    
for _ in range(ni()):
    n = ni()
    S = [ord(a)-96 for a in input()]
    SSA = SA_IS(S)
    ans = 0
    c = 0
    SA = list(SSA[1:])
    #print(SA)
    lcp = LCP(S, n, SSA)
    j = SA.index(0)
    a = [n+1] * n
    a[j] = n
    
    for i in range(j+1, n):
        a[i] = min(a[i-1], lcp[i])
    for i in range(j-1,-1,-1):
        a[i] = min(a[i+1], lcp[i+1])
    #print(j,lcp,a)
    b = []
    for i in range(n):
        l = min(n-SA[i], SA[i])
        if SA[i] == 0:
            c = 1
            continue
        #print(SA[i], l, a[i])
        if a[i] < l:
            ans += c
            if c == 1:
                b.append(SA[i])
        else:
            if n - SA[i] > SA[i]:
                ans += 1
                b.append(SA[i])
    print(ans)
    """print(sorted(b))
    z = 0
    for i in range(1,n):
        if S[i:] > S[:i]:
            print(S[:i], S[i:])
            z += 1
    print(z)"""
0