結果

問題 No.2454 Former < Latter
ユーザー chineristACchineristAC
提出日時 2023-09-01 23:54:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 978 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 87,264 KB
実行使用メモリ 84,824 KB
最終ジャッジ日時 2023-09-01 23:54:40
合計ジャッジ時間 5,388 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
75,028 KB
testcase_01 AC 111 ms
74,804 KB
testcase_02 AC 151 ms
83,668 KB
testcase_03 AC 134 ms
83,772 KB
testcase_04 AC 139 ms
83,448 KB
testcase_05 AC 142 ms
83,588 KB
testcase_06 AC 143 ms
83,580 KB
testcase_07 AC 149 ms
83,744 KB
testcase_08 AC 168 ms
81,544 KB
testcase_09 AC 145 ms
82,432 KB
testcase_10 AC 153 ms
82,536 KB
testcase_11 AC 143 ms
83,672 KB
testcase_12 AC 144 ms
83,580 KB
testcase_13 AC 142 ms
83,752 KB
testcase_14 AC 144 ms
83,636 KB
testcase_15 AC 148 ms
83,256 KB
testcase_16 AC 142 ms
83,496 KB
testcase_17 AC 234 ms
84,824 KB
testcase_18 AC 160 ms
82,116 KB
testcase_19 AC 138 ms
83,536 KB
testcase_20 AC 154 ms
83,712 KB
testcase_21 AC 169 ms
81,712 KB
testcase_22 AC 154 ms
82,132 KB
testcase_23 AC 154 ms
82,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random
from itertools import permutations
from collections import deque
from heapq import *

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def Z_algorithm(s):
    N = len(s)
    Z_alg = [0]*N

    Z_alg[0] = N
    i = 1
    j = 0
    while i < N:
        while i+j < N and s[j] == s[i+j]:
            j += 1
        Z_alg[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < N and k + Z_alg[k]<j:
            Z_alg[i+k] = Z_alg[k]
            k += 1
        i += k
        j -= k
    return Z_alg

for _ in range(int(input())):
    N = int(input())
    S = input()

    Z = Z_algorithm(S)

    ans = 0
    for i in range(1,N):
        if Z[i] == N - i or Z[i] >= i:
            if i < N-i:
                ans += 1
            continue
        if S[Z[i]] < S[Z[i]+i]:
            ans += 1
        else:
            assert S[Z[i]] > S[Z[i]+i]
    print(ans)
0