-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import sys | ||
from collections import defaultdict | ||
input = sys.stdin.readline | ||
|
||
def main(): | ||
adj = None | ||
visited = None | ||
order = None | ||
|
||
def check(order): | ||
order = order[::-1] | ||
for i in range(1, len(order)): | ||
j = i-1 | ||
if adj[order[i]][order[j]]: return False | ||
return order | ||
|
||
def dfs(c): | ||
visited[c] = 1 | ||
for e in adj[c]: | ||
if not visited[e]: | ||
dfs(e) | ||
order.append(c) | ||
|
||
for TC in range(int(input())): | ||
adj = defaultdict(lambda: defaultdict(int)) | ||
N = int(input()) | ||
words = [] | ||
for i in range(N): words.append(input().rstrip()) | ||
for i in range(1, N): # adj initialization | ||
j = i-1 | ||
L = min(len(words[j]), len(words[i])) | ||
for k in range(L): | ||
if words[j][k] != words[i][k]: | ||
adj[words[j][k]][words[i][k]] = 1 | ||
break # following characters are meaningless | ||
|
||
visited = defaultdict(int) | ||
order = [] | ||
for c in reversed('abcdefghijklmnopqrstuvwxyz'): | ||
if not visited[c]: dfs(c) | ||
order = check(order) | ||
print(''.join(order) if order else 'INVALID HYPOTHESIS') | ||
|
||
if __name__ == '__main__': | ||
main() |
3fc536c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AOJI Report
โ Your code has been accepted!
๐ฏ AOJ Result
โ Performance doesn't match. (ยฑ4ms might be a temporary AOJ error)
๐ข Python code has a wider error range in AOJ.
๐ฏ Tested Commit
๐ฏ Submission Log
Click here to extend
1.0.0-alpha.4 | Developer | Workflow