Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week 6] Solved DICTIONARY - profitjean #193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions DEF/DICTIONARY/profitjean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
from collections import defaultdict
input = sys.stdin.readline

def dfs(vertex):
global flag
visited[vertex] = False
adj = graph[vertex]
for node in adj:
if visited[node] == False:
flag = False
elif visited[node] == None:
dfs(node)
sequence.append(vertex)
visited[vertex] = True


case = int(input().rstrip())
for _ in range(case):
graph = defaultdict(set)
N = int(input().rstrip())
words = []
visited = {}
for _ in range(N):
words.append(input().rstrip())
# 이전 문자열과 비교하면서 다르다면 그래프에 추가시켜주기
prev = ""
for word in words:
for i in range(min(len(prev), len(word))):
if prev[i] != word[i]:
graph[prev[i]].add(word[i])
visited[prev[i]] = None
visited[word[i]] = None
break
prev = word
answer = [] #return value
for vertex in visited.keys():
if visited[vertex] == None:
flag = True
sequence = []
dfs(vertex)
if flag:
answer.extend(sequence)
else:
break
if flag:
alphabet = [a if a not in answer else "" for a in 'abcdefghijklmnopqrstuvwxyz']
answer.reverse()
answer = "".join(answer+alphabet)
print(answer)
else:
print("INVALID")