結果

問題 No.430 文字列検索
ユーザー IgrmiIgrmi
提出日時 2021-10-28 02:45:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 667 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 84,580 KB
最終ジャッジ日時 2024-04-16 12:55:36
合計ジャッジ時間 3,827 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
57,600 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 #

def getkmpnext(S):
    N = len(S)
    KMPNext = [-1] * (N + 1)
    j = -1
    for i in range(1, N + 1):
        while j >= 0 and S[i - 1] != S[j]: j = KMPNext[j]
        j += 1
        if i == N or S[i] != S[j]: KMPNext[i] = j
        else: KMPNext[i] = KMPNext[j]
    return KMPNext

def kmp(S, T):
    N = len(S)
    M = len(T)
    KMPNext = getkmpnext(T)
    j = 0
    Res = 0
    for i in range(N):
        while j != KMPNext[0] and S[i] != T[j]: j = KMPNext[j]
        j += 1
        if j == M:
            Res += 1
            j = KMPNext[j]
    return Res

S = input()
Q = int(input())
Ans = 0
for _ in range(Q):
    T = input()
    Ans += kmp(S, T)
print(Ans)
0