結果

問題 No.1005 BOT対策
ユーザー nehan_der_thalnehan_der_thal
提出日時 2020-03-06 22:19:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,180 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 75,940 KB
最終ジャッジ日時 2023-08-04 13:49:07
合計ジャッジ時間 3,366 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,396 KB
testcase_01 AC 63 ms
71,364 KB
testcase_02 AC 65 ms
71,116 KB
testcase_03 AC 65 ms
71,180 KB
testcase_04 AC 64 ms
71,204 KB
testcase_05 AC 64 ms
71,444 KB
testcase_06 AC 64 ms
71,300 KB
testcase_07 AC 63 ms
71,132 KB
testcase_08 AC 64 ms
71,444 KB
testcase_09 AC 64 ms
71,388 KB
testcase_10 AC 63 ms
71,316 KB
testcase_11 AC 67 ms
71,052 KB
testcase_12 AC 66 ms
71,056 KB
testcase_13 AC 66 ms
71,388 KB
testcase_14 AC 66 ms
71,296 KB
testcase_15 AC 67 ms
71,484 KB
testcase_16 AC 64 ms
71,056 KB
testcase_17 AC 65 ms
71,492 KB
testcase_18 AC 66 ms
71,396 KB
testcase_19 AC 67 ms
71,204 KB
testcase_20 AC 66 ms
71,052 KB
testcase_21 AC 67 ms
71,444 KB
testcase_22 AC 69 ms
71,388 KB
testcase_23 AC 64 ms
71,260 KB
testcase_24 AC 66 ms
71,264 KB
testcase_25 AC 67 ms
71,052 KB
testcase_26 AC 66 ms
71,052 KB
testcase_27 AC 64 ms
71,400 KB
testcase_28 AC 69 ms
75,940 KB
testcase_29 AC 63 ms
71,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def partial_match_table(word):
    table = [0] * (len(word) + 1)
    table[0] = -1
    i, j = 0, 1
    while j < len(word):
        matched = word[i] == word[j]
        if not matched and i > 0:
            i = table[i]
        else:
            if matched:
                i += 1
            j += 1
            table[j] = i
    return table

def kmp_search(text, word):
    table = partial_match_table(word)
    i, p = 0, 0
    results = []
    while i < len(text) and p < len(word):
        if text[i] == word[p]:
            i += 1
            p += 1
            if p == len(word):
                p = table[p]
                results.append((i-len(word), i))
        elif p == 0:
            i += 1
        else:
            p = table[p]
    return results

s = input().strip()
t = input().strip()
if len(t) == 1 and t in s:
    import sys
    print(-1)
    sys.exit()
rs = []
for i, (a, b) in enumerate(kmp_search(s, t)):
#    print(a,b-1)
    rs.append((a, 1, i))
    rs.append((b-1, 0, i))
rs.sort()
r = 0
ts = set()
for x, f, i in rs:
    if f == 1:
        ts.add(i)
        continue
    if f == 0 and i in ts:
        ts = set()
        r += 1
        continue
print(r)
0