結果

問題 No.2454 Former < Latter
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2023-09-01 23:19:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 699 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 78,728 KB
最終ジャッジ日時 2024-06-11 05:26:13
合計ジャッジ時間 3,169 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
51,940 KB
testcase_01 WA -
testcase_02 AC 79 ms
78,200 KB
testcase_03 AC 70 ms
77,952 KB
testcase_04 AC 68 ms
78,168 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 76 ms
76,288 KB
testcase_10 AC 72 ms
76,800 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 72 ms
78,080 KB
testcase_14 AC 61 ms
77,952 KB
testcase_15 WA -
testcase_16 AC 59 ms
77,660 KB
testcase_17 AC 440 ms
77,184 KB
testcase_18 WA -
testcase_19 AC 72 ms
78,208 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 75 ms
77,324 KB
testcase_23 AC 80 ms
76,928 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