結果

問題 No.2454 Former < Latter
ユーザー titiatitia
提出日時 2023-09-02 02:16:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 606 ms / 2,000 ms
コード長 853 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 20,312 KB
最終ジャッジ日時 2023-09-02 02:16:16
合計ジャッジ時間 11,300 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,848 KB
testcase_01 AC 16 ms
7,792 KB
testcase_02 AC 564 ms
16,544 KB
testcase_03 AC 477 ms
20,228 KB
testcase_04 AC 425 ms
10,976 KB
testcase_05 AC 443 ms
10,640 KB
testcase_06 AC 413 ms
10,928 KB
testcase_07 AC 490 ms
10,724 KB
testcase_08 AC 391 ms
8,296 KB
testcase_09 AC 257 ms
9,516 KB
testcase_10 AC 325 ms
9,016 KB
testcase_11 AC 606 ms
20,312 KB
testcase_12 AC 547 ms
20,244 KB
testcase_13 AC 593 ms
20,304 KB
testcase_14 AC 591 ms
20,140 KB
testcase_15 AC 362 ms
10,132 KB
testcase_16 AC 373 ms
9,832 KB
testcase_17 AC 498 ms
8,700 KB
testcase_18 AC 356 ms
8,452 KB
testcase_19 AC 363 ms
10,860 KB
testcase_20 AC 447 ms
10,720 KB
testcase_21 AC 460 ms
8,252 KB
testcase_22 AC 302 ms
9,336 KB
testcase_23 AC 306 ms
9,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

T=int(input())
for tests in range(T):
    N=int(input())
    S=input().strip()

    # Z algorithm
    # https://snuke.hatenablog.com/entry/2014/12/03/214243

    LEN=len(S)
    i=1
    j=0
    A=[0]*LEN
    A[0]=LEN

    while i<LEN:
        while i+j<LEN and S[j]==S[i+j]:
            j+=1
        A[i]=j
        
        if j==0:
            i+=1
            continue
        
        k=1
        while i+k<LEN and k+A[k]<j:
            A[i+k]=A[k]
            k+=1
        i+=k
        j-=k

    #print(A)

    ANS=0

    for i in range(1,N):
        L=i
        R=N-i
        
        a=A[i]

        if a==R:
            if L<a:
                ANS+=1
            continue
        if a>=L:
            ANS+=1
            continue

        if S[a]<S[i+a]:
            ANS+=1

    print(ANS)

        
        
0