Trie insert, search만 조금 변형하면 된다.
import sys
input = sys.stdin.readline
from collections import defaultdict
class Node:
def __init__(self):
self.word = False
self.children = {}
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word):
node = self.root
for char in word:
if char not in node.children:
node.children[char] = Node()
node = node.children[char]
same_nick[word] += 1
node.word = True
def search(self, word):
node = self.root
re_word = ''
for char in word:
re_word += char
if char not in node.children:
return re_word
node = node.children[char]
if node.word:
re_word += str(same_nick[re_word]+1)
return re_word
N = int(input())
tree = Trie()
same_nick = defaultdict(int)
for _ in range(N):
word = input().rstrip()
print(tree.search(word))
tree.insert(word)
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 2876 그래픽스 퀴즈 (0) | 2021.09.03 |
---|---|
[백준][Python] 4134 다음 소수 (0) | 2021.09.03 |
[백준][Python] 3673 나눌 수 있는 부분 수열 (0) | 2021.09.02 |
[백준][Python] 1747 소수&펠린드롬 (0) | 2021.09.02 |
[백준][Python] 3613 Java vs C++ (0) | 2021.09.02 |