-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUVA00200.cpp
47 lines (42 loc) · 1.06 KB
/
UVA00200.cpp
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vi;
vector<vi> AdjList;
bool visited[26];
void toposort(int j) {
for (int i = 0; i < AdjList[j].size(); i++) {
if (!visited[AdjList[j][i]]) {
visited[AdjList[j][i]] = true;
toposort(AdjList[j][i]);
}
}
printf("%c", j+'A');
}
int main() {
string n;
vector<string> v;
while (cin >> n && n != "#") {
v.push_back(n);
}
for (int i = 0; i < 26; i++) visited[i] = true;
AdjList.assign(26, vi());
for (int i = 0; i < v.size()-1; i++) {
for (int j = 0; j < v[i].length() && j < v[i+1].length(); j++) {
if (v[i][j] != v[i+1][j]) {
AdjList[v[i+1][j]-'A'].push_back(v[i][j] - 'A');
visited[v[i][j]-'A'] = visited[v[i+1][j]-'A'] = false;
break;
}
}
}
for (int i = 0; i < 26; i++) {
if (!visited[i]) {
visited[i] = true;
toposort(i);
}
}
printf("\n");
return 0;
}