-
Notifications
You must be signed in to change notification settings - Fork 0
/
L05-2.cpp
63 lines (50 loc) · 950 Bytes
/
L05-2.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
#define PUB push_back
#define fi first
#define se second
int N;
bool isAda[30], isVisit[30];
vector<int>adjL[30];
stack<int>ans;
void pecah(string a){
for(int i=0;i<a.length()-1;i++){
isAda[a[i]-'a'] = 1;
adjL[a[i]-'a'].PUB(a[i+1]-'a');
}
isAda[a[a.length()-1]-'a'] = 1;
}
void toposort(int pos){
isVisit[pos] = 1;
for(int nxt:adjL[pos]){
if(!isVisit[nxt])
toposort(nxt);
}
ans.push(pos);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(isAda, 0, sizeof isAda);
cin >> N;
for(int i=0;i<N;i++){
string S;
cin >> S;
pecah(S);
}
for(int i=0;i<=30;i++){
sort(adjL[i].begin(), adjL[i].end(), greater<int>());
}
for(int i=30;i>=0;i--){
if(isAda[i] && !isVisit[i]){
toposort(i);
}
}
while(!ans.empty()){
cout << char(ans.top()+'a');
ans.pop();
}
cout << endl;
return 0;
}