結果
問題 |
No.2761 Substitute and Search
|
ユーザー |
|
提出日時 | 2024-04-29 23:58:09 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,994 bytes |
コンパイル時間 | 740 ms |
コンパイル使用メモリ | 82,364 KB |
実行使用メモリ | 964,072 KB |
最終ジャッジ日時 | 2024-11-29 18:12:17 |
合計ジャッジ時間 | 41,318 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 MLE * 1 |
other | AC * 2 WA * 1 TLE * 4 MLE * 6 |
ソースコード
def convert(str): return [ord(str[i]) - 0x61 for i in range(len(str))] N, L, Q = map(int, input().split()) S = [convert(input().strip()) for _ in range(N)] class Node: __slots__ = ("sum", "children", "parent") def __init__(self): self.sum = 0 self.children = [None] * 26 self.parent = None # (sum, children, parent) trie = Node() by_index_char = [[{} for _ in range(26)] for _ in range(L)] def add(str, amount = 1): cur = trie cur.sum += amount for i in range(L): c = str[i] if not cur.children[c]: node = Node() node.parent = cur cur.children[c] = node by_index_char[i][c][id(node)] = node cur = cur.children[c] cur.sum += amount def substitute(k, c, d): stack = [] for node in by_index_char[k][c].values(): parent = node.parent if not parent: continue parent.children[c] = None if merged := parent.children[d]: stack.append((node, merged, k)) else: parent.children[d] = node by_index_char[k][c].clear() while stack: src, dst, k = stack.pop() dst.sum += src.sum for c in range(26): if src_child := src.children[c]: if dst_child := dst.children[c]: by_index_char[k + 1][c].pop(id(src_child)) stack.append((src_child, dst_child, k + 1)) else: src_child.parent = dst dst.children[c] = src_child def count_prefix(t): cur = trie for c in t: cur = cur.children[c] if not cur: return 0 return cur.sum for s in S: add(s) for _ in range(Q): values = input().split() if values[0] == "1": k = int(values[1]) - 1 c = ord(values[2]) - 0x61 d = ord(values[3]) - 0x61 substitute(k, c, d) else: t = convert(values[1]) print(count_prefix(t))