結果

問題 No.2454 Former < Latter
ユーザー ニックネーム
提出日時 2023-09-01 22:03:02
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,391 ms / 2,000 ms
コード長 518 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 22,728 KB
最終ジャッジ日時 2025-01-03 08:39:55
合計ジャッジ時間 9,866 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

def z_algorithm(s):
    if not s: return []
    n = len(s); z = [0]*n; z[0] = n; i = 1; j = 0
    while i<n:
        while i+j<n and s[j]==s[i+j]: j += 1
        if not j: i += 1; continue
        z[i] = j; k = 1
        while i+k<n and k+z[k]<j: z[i+k] = z[k]; k += 1
        i += k; j -= k
    return z
for _ in range(int(input())):
    n = int(input()); s = input()
    z = z_algorithm(s); ans = 0
    for i in range(1,n):
        v = min(z[i],i)
        if i+v!=n and (v==i or s[v]<s[i+v]): ans += 1
    print(ans)
0