結果

問題 No.2454 Former < Latter
ユーザー rlangevinrlangevin
提出日時 2023-09-01 23:24:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 858 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 90,160 KB
最終ジャッジ日時 2023-09-01 23:24:47
合計ジャッジ時間 4,184 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,432 KB
testcase_01 AC 68 ms
71,296 KB
testcase_02 AC 108 ms
88,376 KB
testcase_03 AC 103 ms
88,616 KB
testcase_04 AC 100 ms
88,880 KB
testcase_05 AC 100 ms
88,736 KB
testcase_06 AC 106 ms
88,632 KB
testcase_07 AC 104 ms
88,592 KB
testcase_08 AC 116 ms
78,004 KB
testcase_09 AC 106 ms
82,160 KB
testcase_10 AC 109 ms
83,688 KB
testcase_11 AC 107 ms
88,568 KB
testcase_12 AC 100 ms
88,664 KB
testcase_13 AC 98 ms
88,816 KB
testcase_14 AC 101 ms
88,308 KB
testcase_15 AC 109 ms
90,160 KB
testcase_16 AC 109 ms
85,396 KB
testcase_17 AC 209 ms
80,632 KB
testcase_18 AC 113 ms
77,692 KB
testcase_19 AC 98 ms
88,724 KB
testcase_20 AC 124 ms
88,556 KB
testcase_21 AC 126 ms
77,956 KB
testcase_22 AC 109 ms
82,044 KB
testcase_23 AC 113 ms
84,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def Z_algorithm(s):
    n = len(s)
    A = [n] + [0] * (n-1)
    i, j = 1, 0
    while i < n:
        while i + j < n and s[j] == s[i+j]:
            j += 1
        A[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < n and k + A[k] < j:
            A[i+k] = A[k]
            k += 1
        i += k
        j -= k
    return A

T = int(input())
for _ in range(T):
    N = int(input())
    S = list(input().rstrip())
    S = [ord(s) - ord("a") for s in S]
    L = Z_algorithm(S)
    ans = 0
    for i in range(1, N):
        L[i] = min(i, L[i])
        if i == L[i] and 2 * i < N:
            ans += 1
        else:
            if i + L[i] <= N - 1:
                if S[L[i]] < S[i + L[i]]:
                    ans += 1
    print(ans)
            
            
        
0