-
Notifications
You must be signed in to change notification settings - Fork 0
/
200.cpp
67 lines (60 loc) · 1.34 KB
/
200.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Problem : 200 - Rare Order
* Writer : Mehadi Hasan Menon.
* Verdict : Accepted.
* Date : 13.02.2018
*/
#include <iostream>
#include <vector>
#include <iostream>
#include <set>
using namespace std;
vector <vector <char> > adj_list;
vector <char> top_sort;
bool is_vis[256];
void dfs(char u)
{
is_vis[u] = true;
int sz = adj_list[u].size();
for(int i = 0; i < sz; i++)
{
char v = adj_list[u][i];
if(is_vis[v] == false) {
dfs(v);
}
}
top_sort.push_back(u);
}
int main()
{
freopen("input.txt", "r+", stdin);
string str1, str2;
adj_list.resize(256);
set <char> nodes;
cin >> str1;
while(cin >> str2 && str2 != "#")
{
for(int i = 0; i < str1.length() && i < str2.length(); i++)
{
if(str1[i] != str2[i])
{
adj_list[str1[i]].push_back(str2[i]);
nodes.insert(str1[i]);
nodes.insert(str2[i]);
break;
}
}
str1 = str2;
}
// cout << "node size: " << nodes.size() << endl;
for(auto it = nodes.begin(); it != nodes.end(); ++it) {
if(is_vis[*it] == false) {
dfs(*it);
}
}
for(int i = top_sort.size() - 1; i >= 0; i--) {
cout << top_sort[i];
}
cout << endl;
return 0;
}