結果

問題 No.430 文字列検索
ユーザー roarisroaris
提出日時 2020-07-29 22:17:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 883 bytes
コンパイル時間 255 ms
コンパイル使用メモリ 87,156 KB
実行使用メモリ 78,188 KB
最終ジャッジ日時 2023-09-12 21:41:39
合計ジャッジ時間 6,478 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
78,188 KB
testcase_01 TLE -
testcase_02 TLE -
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 = sys.stdin.readline

def make_table(s):
    n = len(s)
    res = [-1]*(n+1)
    j = -1
    
    for i in range(n):
        while j>=0 and s[i]!=s[j]:
            j = res[j]
        
        j += 1
        res[i+1] = j
    
    return res

def kmp(s, w): #s中のwと一致する箇所の先頭インデックスのリストを作成
    #table = make_table(w)
    res = []
    m, i, n = 0, 0, len(s)
    
    while m+i<n:
        if w[i]==s[m+i]:
            i += 1
            
            if i==len(w):
                res.append(m)
                m = m+i-table[i]
                i = table[i]
        else:
            m = m+i-table[i]
            
            if i>0:
                i = table[i]
    
    return res

S = input()[:-1]
M = int(input())
table = make_table(S)
ans = 0

for _ in range(M):
    C = input()[:-1]
    ans += len(kmp(S, C))

print(ans)
0