結果
| 問題 |
No.430 文字列検索
|
| コンテスト | |
| ユーザー |
neterukun
|
| 提出日時 | 2021-05-24 01:33:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 340 ms / 2,000 ms |
| コード長 | 2,155 bytes |
| コンパイル時間 | 176 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 80,768 KB |
| 最終ジャッジ日時 | 2024-11-10 00:55:42 |
| 合計ジャッジ時間 | 3,868 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 14 |
ソースコード
class PatriciaNode:
def __init__(self, string=None):
self.data = string
self.child = {}
self.valid = False
def set_child(self, string):
self.child[string[0]] = PatriciaNode(string)
return self.child[string[0]]
def get_child(self, s):
if s not in self.child:
return None
return self.child[s]
class Patricia:
def __init__(self):
self.root = PatriciaNode()
def _longest_match(self, string):
ptr = self.root
i = 0
while i < len(string):
ptrch = ptr.get_child(string[i])
if ptrch is None:
break
j = 1
while j < len(ptrch .data):
if i + j == len(string) or string[i + j] != ptrch .data[j]:
return ptrch , i + j, j
j += 1
i += j
ptr = ptrch
return ptr, i, 0
def search(self, string):
ptr, match, sub_match = self._longest_match(string)
if len(string) == match and sub_match == 0:
return ptr.valid
return False
def insert(self, string):
ptr, match, sub_match = self._longest_match(string)
if sub_match > 0:
newptr = PatriciaNode(ptr.data[sub_match:])
newptr.child = ptr.child
newptr.valid = ptr.valid
ptr.data = ptr.data[:sub_match]
ptr.child = {newptr.data[0]: newptr}
if match < len(string):
ptr.valid = False
ptr = ptr.set_child(string[match:])
ptr.valid = True
else:
ptr.valid = True
else:
if match == len(string):
ptr.valid = True
else:
ptr = ptr.set_child(string[match:])
ptr.valid = True
s = input()
m = int(input())
c = [input() for i in range(m)]
tr = Patricia()
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)
neterukun