importsysfromtypingimportListclassTrie:def__init__(self):self.root={}self.answer:List[str]=[]definsert(self,food):current_node=self.rootforeachinfood:ifeachnotincurrent_node:current_node[each]={}# 예전에 했던 딕셔너리 안에 딕셔너리 넣는 방식
current_node=current_node[each]# 0으로 찾는 거임 그때처럼
current_node[0]=Truedefappendlist(self,cnt,current_node):if0incurrent_node:returnforeachincurrent_node:self.answer.append('--'*cnt+each)self.appendlist(cnt+1,current_node[each])defsolution(foods:List[List[str]])->List[str]:t=Trie()forfoodinsorted(foods):t.insert(food)t.appendlist(0,t.root)returnt.answerN=int(sys.stdin.readline().rstrip())foods=[]for_inrange(N):foods.append(list(sys.stdin.readline().rstrip().split())[1:])print(*solution(foods),sep='\n')
Leave a comment