結果

問題 No.2454 Former < Latter
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-09-01 23:19:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 699 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 79,504 KB
最終ジャッジ日時 2023-09-01 23:19:42
合計ジャッジ時間 5,449 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,448 KB
testcase_01 WA -
testcase_02 AC 116 ms
79,164 KB
testcase_03 AC 97 ms
79,136 KB
testcase_04 AC 104 ms
79,220 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 104 ms
78,252 KB
testcase_10 AC 113 ms
78,128 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 100 ms
79,196 KB
testcase_14 AC 100 ms
79,220 KB
testcase_15 WA -
testcase_16 AC 101 ms
78,684 KB
testcase_17 AC 529 ms
78,084 KB
testcase_18 WA -
testcase_19 AC 95 ms
79,384 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 117 ms
77,668 KB
testcase_23 AC 110 ms
78,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Z_algorithm(s):
    
    n = len(s)
    if n == 0:
        return []

    z = [0]*n
    j = 0
    for i in range(1,n):
        z[i] = 0 if j + z[j] <= i else min(j+z[j]-i,z[i-j])
        while i + z[i] < n and s[z[i]] == s[i+z[i]]:
            z[i] += 1
            if j + z[j] < i + z[i]:
                j = i
    z[0] = n
    return z


def solve():

    n = int(input())

    S = input()

    Z = Z_algorithm(S)

    ans = 0

    for i in range(1,n):
        z = Z[i]

        if z == n-i:
            if i+1 <= n-i:
                ans += 1
        else:
            if S[z] < S[i+z]:
                ans += 1
    print(ans)

    # print(Z)

t = int(input())
for _ in range(t):
    solve()
0