結果

問題 No.2454 Former < Latter
ユーザー ニックネームニックネーム
提出日時 2023-09-01 22:03:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,342 ms / 2,000 ms
コード長 518 bytes
コンパイル時間 115 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 20,028 KB
最終ジャッジ日時 2023-09-01 22:03:13
合計ジャッジ時間 9,621 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,792 KB
testcase_01 AC 16 ms
7,784 KB
testcase_02 AC 393 ms
16,172 KB
testcase_03 AC 309 ms
20,028 KB
testcase_04 AC 325 ms
10,792 KB
testcase_05 AC 321 ms
10,540 KB
testcase_06 AC 312 ms
10,608 KB
testcase_07 AC 346 ms
10,488 KB
testcase_08 AC 289 ms
8,276 KB
testcase_09 AC 217 ms
9,372 KB
testcase_10 AC 253 ms
8,892 KB
testcase_11 AC 393 ms
19,992 KB
testcase_12 AC 395 ms
20,004 KB
testcase_13 AC 407 ms
20,004 KB
testcase_14 AC 409 ms
19,980 KB
testcase_15 AC 294 ms
10,020 KB
testcase_16 AC 304 ms
9,800 KB
testcase_17 AC 1,342 ms
7,840 KB
testcase_18 AC 285 ms
7,880 KB
testcase_19 AC 311 ms
10,540 KB
testcase_20 AC 348 ms
10,540 KB
testcase_21 AC 333 ms
8,304 KB
testcase_22 AC 227 ms
9,084 KB
testcase_23 AC 234 ms
9,404 KB
権限があれば一括ダウンロードができます

ソースコード

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