結果

問題 No.1005 BOT対策
ユーザー nehan_der_thal
提出日時 2020-03-06 22:09:43
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,051 bytes
コンパイル時間 221 ms
コンパイル使用メモリ 81,772 KB
実行使用メモリ 60,436 KB
最終ジャッジ日時 2024-10-14 10:40:38
合計ジャッジ時間 2,669 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 16 WA * 11
権限があれば一括ダウンロードができます

ソースコード

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()
rs = []
for a, b in kmp_search(s, t):
    rs.append((a, 0))
    rs.append((b-1, 1))
rs.sort()
c = 0
r = 0
for x, f in rs:
    if f == 0:
        c += 1
        continue
    if f == 1 and c > 0:
        c = 0
        r += 1
        continue
print(r)
0