alphabet = 'abcdefghijklmnopqrstuvwxyz'
def rule_maker(r1, r2):
for i, j in zip(r1, r2):
if i != j: sought = (i, j); break
else:
if len(r1) <= len(r2): sought = 1
else: sought = 0
return sought
def visit(graph, firth, temp, perm, final, unmarked):
global t
if temp[ord(firth)-97]: t = 1
elif not(perm[ord(firth)-97]):
temp[ord(firth)-97] = 1
for forth in graph[firth]: m = visit(graph, forth, temp, perm, final, unmarked)
unmarked.discard(firth)
perm[ord(firth)-97] = 1
temp[ord(firth)-97] = 0
final.append(firth)
n = int(input())
words = [input() for i in range(n)]
neu = [int(i) for i in input().split(" ")]
final_words = [words[i-1] for i in neu]
diktafon = {}
for i in range(97, 123): diktafon[chr(i)] = []
f = 1
rules = 0
for i, w0 in enumerate(final_words):
for w1 in final_words[(i+1):]:
s = rule_maker(w0, w1)
if s == 0: f = 0; break
elif s != 1:
diktafon[s[0]].append(s[1])
rules = 1
if f == 0: print("NE")
elif not(rules): print("DA"); print(alphabet)
else:
t = 0
temp = [0]*26; perm = [0]*26
final = []
unmarked = set(chr(i) for i in range(97, 123))
while unmarked:
s = unmarked.pop()
visit(diktafon, s, temp, perm, final, unmarked)
if t: print("NE"); break
else:
final.reverse()
print("DA")
makn = set()
sa = set(alphabet)
k = [0]*26
for i, l in enumerate(final):
k[ord(l)-97] = alphabet[i]
sa -= {alphabet[i]}
for i in range(26):
if not(k[i]): k[i] = sa.pop()
print("".join(k))
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
3428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
3560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
3624 KB |
Output is correct |
2 |
Correct |
25 ms |
3692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
24 ms |
3692 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
3736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
27 ms |
3736 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
29 ms |
3736 KB |
Execution failed because the return code was nonzero |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
3736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
3736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
3736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |