結果
問題 | No.430 文字列検索 |
ユーザー | neterukun |
提出日時 | 2021-06-12 17:02:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 161 ms / 2,000 ms |
コード長 | 1,831 bytes |
コンパイル時間 | 466 ms |
コンパイル使用メモリ | 82,380 KB |
実行使用メモリ | 86,680 KB |
最終ジャッジ日時 | 2024-05-09 20:32:19 |
合計ジャッジ時間 | 2,642 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,760 KB |
testcase_01 | AC | 161 ms
86,680 KB |
testcase_02 | AC | 98 ms
77,596 KB |
testcase_03 | AC | 96 ms
77,568 KB |
testcase_04 | AC | 41 ms
54,016 KB |
testcase_05 | AC | 37 ms
54,272 KB |
testcase_06 | AC | 36 ms
53,888 KB |
testcase_07 | AC | 37 ms
53,760 KB |
testcase_08 | AC | 70 ms
72,704 KB |
testcase_09 | AC | 52 ms
64,640 KB |
testcase_10 | AC | 56 ms
67,840 KB |
testcase_11 | AC | 140 ms
82,776 KB |
testcase_12 | AC | 143 ms
83,488 KB |
testcase_13 | AC | 156 ms
83,712 KB |
testcase_14 | AC | 135 ms
81,516 KB |
testcase_15 | AC | 125 ms
80,352 KB |
testcase_16 | AC | 120 ms
80,384 KB |
testcase_17 | AC | 119 ms
80,508 KB |
ソースコード
from collections import deque class Node: def __init__(self): self.child = {} self.failure = None self.valid = 0 def set_child(self, s): self.child[s] = Node() def get_child(self, s): if s not in self.child: return None return self.child[s] class AhoCorasick: def __init__(self): self.root = Node() def add(self, pattern): ptr = self.root for s in pattern: if ptr.get_child(s) is None: ptr.set_child(s) ptr = ptr.get_child(s) ptr.valid += 1 def build_failure(self): queue = deque() for char in self.root.child: ptrch = self.root.child[char] ptrch.failure = self.root queue.append(ptrch) while queue: ptr = queue.popleft() ptr.valid += ptr.failure.valid for char in ptr.child: ptrch = ptr.child[char] queue.append(ptrch) f = ptr.failure while f is not None and f.get_child(char) is None: f = f.failure if f is None: ptrch.failure = self.root else: ptrch.failure = f.get_child(char) def match_cnt(self, text): ptr = self.root res = 0 for char in text: while ptr is not None and ptr.get_child(char) is None: ptr = ptr.failure if ptr is None: ptr = self.root else: ptr = ptr.get_child(char) res += ptr.valid return res text = input() m = int(input()) c = [input() for i in range(m)] ac = AhoCorasick() for string in c: ac.add(string) ac.build_failure() print(ac.match_cnt(text))