結果

問題 No.2454 Former < Latter
ユーザー sotanishysotanishy
提出日時 2023-09-01 21:53:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 504 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 80,376 KB
最終ジャッジ日時 2023-09-01 21:53:40
合計ジャッジ時間 4,169 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,212 KB
testcase_01 AC 71 ms
71,156 KB
testcase_02 AC 109 ms
79,696 KB
testcase_03 AC 99 ms
79,456 KB
testcase_04 AC 96 ms
79,600 KB
testcase_05 AC 96 ms
79,608 KB
testcase_06 AC 99 ms
79,372 KB
testcase_07 AC 105 ms
79,524 KB
testcase_08 AC 127 ms
77,508 KB
testcase_09 AC 102 ms
78,280 KB
testcase_10 AC 107 ms
78,420 KB
testcase_11 AC 102 ms
79,532 KB
testcase_12 AC 101 ms
79,492 KB
testcase_13 AC 102 ms
79,416 KB
testcase_14 AC 103 ms
79,440 KB
testcase_15 AC 103 ms
79,124 KB
testcase_16 AC 105 ms
79,156 KB
testcase_17 AC 198 ms
80,376 KB
testcase_18 AC 116 ms
77,580 KB
testcase_19 AC 96 ms
79,792 KB
testcase_20 AC 109 ms
79,620 KB
testcase_21 AC 122 ms
78,196 KB
testcase_22 AC 105 ms
78,472 KB
testcase_23 AC 104 ms
78,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


def z_array(s):
    n = len(s)
    z = [0] * n
    z[0] = n
    l = r = 0
    for i in range(1, n):
        k = i - l
        if i <= r and z[k] < r - i + 1:
            z[i] = z[k]
        else:
            l = i
            if i > r:
                r = i
            while r < n and s[r - l] == s[r]:
                r += 1
            r -= 1
            z[i] = r - l + 1
    return z


T = int(input())
for _ in range(T):
    N = int(input())
    S = input().rstrip()
    z = z_array(S)
    cnt = 0
    for i in range(1, N):
        if z[i] > i:
            cnt += 1
        elif z[i] == i and i+z[i] < N:
            cnt += 1
        elif i+z[i] < N and S[z[i]] < S[i+z[i]]:
            cnt += 1
    print(cnt)
0