Skip to content

Commit 4e2a297

Browse files
committed
DICTIONARY 224ms
1 parent 38cd040 commit 4e2a297

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

DEF/DICTIONARY/profitjean.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
from collections import defaultdict
3+
input = sys.stdin.readline
4+
5+
def dfs(vertex):
6+
global flag
7+
visited[vertex] = False
8+
adj = graph[vertex]
9+
for node in adj:
10+
if visited[node] == False:
11+
flag = False
12+
elif visited[node] == None:
13+
dfs(node)
14+
sequence.append(vertex)
15+
visited[vertex] = True
16+
17+
18+
case = int(input().rstrip())
19+
for _ in range(case):
20+
graph = defaultdict(set)
21+
N = int(input().rstrip())
22+
words = []
23+
visited = {}
24+
for _ in range(N):
25+
words.append(input().rstrip())
26+
# 이전 문자열과 비교하면서 다르다면 그래프에 추가시켜주기
27+
prev = ""
28+
for word in words:
29+
for i in range(min(len(prev), len(word))):
30+
if prev[i] != word[i]:
31+
graph[prev[i]].add(word[i])
32+
visited[prev[i]] = None
33+
visited[word[i]] = None
34+
break
35+
prev = word
36+
answer = [] #return value
37+
for vertex in visited.keys():
38+
if visited[vertex] == None:
39+
flag = True
40+
sequence = []
41+
dfs(vertex)
42+
if flag:
43+
answer.extend(sequence)
44+
else:
45+
break
46+
if flag:
47+
alphabet = [a if a not in answer else "" for a in 'abcdefghijklmnopqrstuvwxyz']
48+
answer.reverse()
49+
answer = "".join(answer+alphabet)
50+
print(answer)
51+
else:
52+
print("INVALID")
53+
54+

0 commit comments

Comments
 (0)