結果
問題 |
No.430 文字列検索
|
ユーザー |
![]() |
提出日時 | 2020-05-05 02:25:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 336 ms / 2,000 ms |
コード長 | 1,705 bytes |
コンパイル時間 | 235 ms |
コンパイル使用メモリ | 82,076 KB |
実行使用メモリ | 85,088 KB |
最終ジャッジ日時 | 2024-11-10 00:43:44 |
合計ジャッジ時間 | 4,095 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 14 |
ソースコード
class TrieNode: """Trie用のノード用クラスであり、 子供のポインタ(辞書で管理)とノードが有効かどうかを持つ""" def __init__(self, s): self.child = {} self.valid = False def set_child(self, s): self.child[s] = TrieNode(s) def get_child(self, s): if s not in self.child: return None return self.child[s] class Trie: """Trie木: 文字列の検索、追加、削除をO(|検索する文字列長さ|)で行う""" def __init__(self): self.root = TrieNode(None) def search(self, string: str) -> bool: """集合に文字列が存在するかどうかを返す""" ptr = self.root for s in string: if ptr.get_child(s) is None: return False ptr = ptr.get_child(s) return ptr.valid def insert(self, string: str): """集合に文字列を追加する""" ptr = self.root for s in string: if ptr.get_child(s) is None: ptr.set_child(s) ptr = ptr.get_child(s) ptr.valid = True def delete(self, string: str): """集合から文字列を削除する""" ptr = self.root for s in string: if ptr.get_child(s) is None: return ptr = ptr.get_child(s) ptr.valid = False s = input() m = int(input()) c = [input() for i in range(m)] tr = Trie() for string in c: tr.insert(string) ans = 0 for i in range(len(s)): for length in range(1, 11): if i + length > len(s): break if tr.search(s[i:i + length]): ans += 1 print(ans)