This documentation is automatically generated by online-judge-tools/verification-helper
Patricia()
空の Patricia 木を初期構築する。計算量 $O(1)$
search(string: str) -> bool
長さ $S$ の文字列 string
が Patricia 木に存在しているかどうかを返す。計算量 $O(S)$
insert(string: str) -> bool
長さ $S$ の文字列 string
を Patricia 木に追加する。追加に成功した場合は True
を、失敗した場合 (既に string
が Patricia 木に存在していた場合) は False
を返す。計算量 $O(S)$
delete(string: str) -> bool
長さ $S$ の文字列 string
を Patricia 木から削除する。削除に成功した場合は True
を、失敗した場合 (string
が Patricia 木に存在していなかった場合) は False
を返す。計算量 $O(S)$
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 len(string) == match and sub_match == 0 and ptr.valid:
return False
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}
ptr.valid = False
if match < len(string):
ptr = ptr.set_child(string[match:])
ptr.valid = True
return True
def delete(self, string):
ptr, match, sub_match = self._longest_match(string)
if len(string) == match and sub_match == 0 and ptr.valid:
ptr.valid = False
return True
return False
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/hostedtoolcache/Python/3.12.4/x64/lib/python3.12/site-packages/onlinejudge_verify/languages/python.py", line 96, in bundle
raise NotImplementedError
NotImplementedError