結果

問題 No.2454 Former < Latter
ユーザー chineristAC
提出日時 2023-09-01 23:54:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 978 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 81,664 KB
実行使用メモリ 79,616 KB
最終ジャッジ日時 2025-01-03 13:27:18
合計ジャッジ時間 3,762 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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