結果

問題 No.2454 Former < Latter
ユーザー mkawa2mkawa2
提出日時 2023-09-01 22:47:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,557 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 81,524 KB
最終ジャッジ日時 2023-09-01 22:47:34
合計ジャッジ時間 4,295 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,204 KB
testcase_01 AC 74 ms
71,340 KB
testcase_02 AC 106 ms
79,480 KB
testcase_03 AC 95 ms
78,960 KB
testcase_04 AC 99 ms
79,352 KB
testcase_05 AC 101 ms
79,300 KB
testcase_06 AC 127 ms
79,508 KB
testcase_07 AC 106 ms
79,264 KB
testcase_08 AC 116 ms
77,132 KB
testcase_09 AC 96 ms
77,416 KB
testcase_10 AC 107 ms
78,284 KB
testcase_11 AC 100 ms
79,244 KB
testcase_12 AC 98 ms
79,320 KB
testcase_13 AC 99 ms
79,460 KB
testcase_14 AC 100 ms
79,200 KB
testcase_15 AC 123 ms
78,656 KB
testcase_16 AC 98 ms
78,900 KB
testcase_17 AC 220 ms
81,524 KB
testcase_18 AC 123 ms
77,960 KB
testcase_19 AC 97 ms
79,696 KB
testcase_20 AC 109 ms
79,332 KB
testcase_21 AC 120 ms
77,216 KB
testcase_22 AC 106 ms
78,408 KB
testcase_23 AC 130 ms
78,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

def z_algorithm(target):
    len_t = len(target)
    lcp = [-1]*len_t
    top = 1
    right = 0
    lcp[0] = 0
    while top < len_t:
        while top+right < len_t and target[right] == target[top+right]:
            right += 1
        lcp[top] = right
        left = 1
        if right == 0:
            top += 1
            continue
        while left+lcp[left] < right and left < right:
            lcp[top+left] = lcp[left]
            left += 1
        top += left
        right -= left
    return lcp

def solve():
    n=II()
    s=SI()
    lcp=z_algorithm(s)
    # print(lcp)
    ans=0
    for i in range(1,n):
        l=lcp[i]
        r=lcp[i]+i
        if l>=i or r>=n:
            ans+=i<n-i
        else:
            ans+=s[l]<s[r]
        # print(i,ans)
    print(ans)

for _ in range(II()):solve()
0