結果
| 問題 |
No.2761 Substitute and Search
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-05 17:07:14 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,744 bytes |
| コンパイル時間 | 446 ms |
| コンパイル使用メモリ | 82,232 KB |
| 実行使用メモリ | 849,216 KB |
| 最終ジャッジ日時 | 2024-11-29 18:16:19 |
| 合計ジャッジ時間 | 27,243 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 MLE * 7 |
ソースコード
class TrieNode:
__slots__ = ("sum", "children")
def __init__(self):
self.sum = 0
self.children = [None] * 26
def merge(self, other):
self.sum += other.sum
for c in range(26):
rhs = other.children[c]
if rhs:
lhs = self.children[c]
if lhs:
lhs.merge(rhs)
else:
self.children[c] = rhs
class Trie:
__slots__ = ("root", "subst")
def __init__(self, l):
self.root = TrieNode()
self.subst = [[i % 26] for i in range(l * 26)]
def add(self, s):
cur = self.root
cur.sum += 1
for k in range(len(s)):
c = ord(s[k]) - 0x61
if not cur.children[c]:
node = TrieNode()
cur.children[c] = node
cur = cur.children[c]
cur.sum += 1
def substitute(self, k, c, d):
c = ord(c) - 0x61
d = ord(d) - 0x61
x, y = self.subst[k * 26 + d], self.subst[k * 26 + c]
if len(x) < len(y): x, y = y, x
for v in y: x.append(v)
self.subst[k * 26 + d] = x
self.subst[k * 26 + c] = []
def search(self, s):
cur = self.root
for k in range(len(s)):
d = ord(s[k]) - 0x61
last = None
for c in self.subst[k * 26 + d]:
node = cur.children[c]
if node:
if last:
last.merge(node)
cur.children[c] = None
else:
last = node
if not last:
return 0
cur = last
return cur.sum
N, L, Q = map(int, input().split())
trie = Trie(L)
for _ in range(N):
trie.add(input().strip())
for _ in range(Q):
query = input().strip().split()
if query[0] == "1":
k = int(query[1]) - 1
c = query[2]
d = query[3]
trie.substitute(k, c, d)
else:
print(trie.search(query[1]))