結果

問題 No.430 文字列検索
ユーザー rpy3cpp
提出日時 2016-10-07 23:46:25
言語 Python2
(2.7.18)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 873 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-11-10 00:09:59
合計ジャッジ時間 1,480 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

input = raw_input
range = xrange

def read_data():
    S = input()
    M = int(input())
    Cs = [input() for _ in range(M)]
    return S, M, Cs

def solve(S, M, Cs):
    base = ord('A') - 1
    mask10 = (1 << 50) - 1
    mask = init_mask(10)
    pool = init_pool(Cs)
    count = 0
    val = 0
    for c in S:
        val = ((val << 5) + ord(c) - base) & mask10
        for i in range(10):
            if (val & mask[i]) in pool[i]:
                count += 1
    return count

def init_mask(n):
    mask = []
    for i in range(1, n + 1):
        mask.append((1 << (5 * i)) - 1)
    return mask

def init_pool(Cs):
    base = ord('A') - 1
    pool = [set() for _ in range(10)]
    for C in Cs:
        val = 0
        for c in C:
            val = (val << 5) + ord(c) - base
        pool[len(C) - 1].add(val)
    return pool

S, M, Cs = read_data()
print(solve(S, M, Cs))
0