結果

問題 No.430 文字列検索
ユーザー zkouzkou
提出日時 2020-11-13 17:01:03
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,169 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 81,692 KB
最終ジャッジ日時 2023-09-30 02:13:07
合計ジャッジ時間 4,194 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,168 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

S = input()
M = int(input())
Cs = [input() for _ in range(M)]

def kmp_count(string, word):
    # バックトラックテーブルの構築
    # T[i] : word の先頭が string[m] で、
    #        i 番目で不一致だったとき(i.e. string[m + i] != word[i])、
    #        word の先頭を m + i - T[i] にずらす。
    assert len(word) >= 1
    T = [0] * (len(word) + 1)
    T[0] = -1
    i = 2
    j = 0
    while i <= len(word):
        if word[i - 1] == word[j]:
            T[j] = j + 1
            i += 1
            j += 1
        elif j > 0:
            j = T[j]
        else:
            T[i] = 0
            i += 1
    # print(word)
    # print(T)
    # 文字列検索
    m = 0
    i = 0
    ret = 0
    while m + i < len(string):
        if word[i] == string[m + i]:
            i += 1
            if i == len(word):
                ret += 1
                m = m + i - T[i]
                i = T[i]
        else:
            m = m + i - T[i]
            if i > 0:
                i = T[i]
    # print(ret)
    return ret

ans = 0
for C in Cs:
    ans += kmp_count(S, C)

print(ans)
0