-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoxAndNames.java
122 lines (105 loc) · 2.94 KB
/
FoxAndNames.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/** https://codeforces.com/problemset/problem/510/C #topological-sort #graph #dfs */
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;
class FoxAndNames {
public static boolean topoSorting(
ArrayList<ArrayList<Integer>> graph, int[] inDegree, ArrayList<Integer> resultArr) {
PriorityQueue<Integer> zeroInDegree = new PriorityQueue<>();
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
zeroInDegree.add(i);
}
}
while (!zeroInDegree.isEmpty()) {
int node = zeroInDegree.poll();
resultArr.add(node);
visitedArr[node] = true;
for (int neighbour : graph.get(node)) {
if (!visitedArr[neighbour]) {
inDegree[neighbour] -= 1;
if (inDegree[neighbour] == 0) {
zeroInDegree.add(neighbour);
}
}
}
}
boolean ret = true;
for (int degree : inDegree) {
if (degree > 0) {
ret = false;
break;
}
}
return ret;
}
// return -1 if name 2 is prefix of name1, 100(max size of a string) if name1 is prefix of name2,
// or index at which name1 differ name2.
static int getDiffIndex(String name1, String name2) {
int ret = -1;
int idx = 0;
if (name1.contains(name2)) {
return ret;
}
while (idx < name1.length() && idx < name2.length()) {
if (name1.charAt(idx) != name2.charAt(idx)) {
ret = idx;
break;
}
idx++;
}
if (ret == -1 && name1.length() <= name2.length()) {
ret = 100;
}
return ret;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sz = 'z' - 'a' + 1;
// special case
if (n == 1) {
for (int i = 'a'; i < sz; i++) {
System.out.print(String.format("%c", i));
}
return;
}
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<>());
}
ArrayList<Integer> resultArr = new ArrayList<>();
int[] inDegree = new int[sz];
int diffIdx;
int u, v;
String name1 = sc.next();
String name2;
for (int i = 1; i < n; i++) {
name2 = sc.next();
diffIdx = getDiffIndex(name1, name2);
if (diffIdx == -1) {
System.out.println("Impossible");
return;
} else if (diffIdx == 100) {
continue;
}
u = name1.charAt(diffIdx) - 'a';
v = name2.charAt(diffIdx) - 'a';
name1 = name2;
graph.get(u).add(v);
inDegree[v] += 1;
}
boolean isPossible = topoSorting(graph, inDegree, resultArr);
if (isPossible) {
for (int i = 0; i < resultArr.size(); i++) {
System.out.print(String.format("%c", resultArr.get(i) + 'a'));
}
System.out.println();
} else {
System.out.println("Impossible");
}
return;
}
}