forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcezar.cpp
95 lines (79 loc) · 1.97 KB
/
cezar.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
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
#include <bits/stdc++.h>
using namespace std;
// Takes in two strings, adds an edge if possible
// returns whether or not it is possible using this config
bool cmp(vector<vector<bool>>& adj, vector<int>& deg, string s1, string s2) {
for(int i = 0; i < min(s1.size(), s2.size()); i++) {
if(s1[i] != s2[i]) {
if(!adj[s1[i]-'a'][s2[i]-'a']) {
adj[s1[i]-'a'][s2[i]-'a'] = true;
deg[s2[i]-'a']++;
}
return true;
}
}
return s1.size() < s2.size();
}
// From the toposort order, build the answer
string convert(string s) {
string ans(26, 'a');
for(int i = 0; i < s.size(); i++) {
ans[s[i]-'a'] = i+'a';
}
return ans;
}
int main() {
int n;
cin >> n;
vector<string> input(n);
for(auto& i : input) {
cin >> i;
}
vector<string> sorted(n);
for(int i = 0; i < n; i++) {
int t;
cin >> t;
t--;
sorted[i] = input[t];
}
vector<int> deg(26, 0);
vector<vector<bool>> adj;
adj.resize(26, vector<bool>(26, false));
// For each pair, do a string compare, and add edge or detect
// if impossible
bool works = true;
for(int i = 1; i < n; i++) {
works &= cmp(adj, deg, sorted[i-1], sorted[i]);
}
// Prepare toposort
queue<int> q;
for(int i = 0; i < 26; i++) {
if(deg[i] == 0) {
q.push(i);
}
}
string order = "";
// Run toposort
while(!q.empty()) {
int curr = q.front();
q.pop();
order.push_back('a'+curr);
for(int i = 0; i < 26; i++) {
if(adj[curr][i]) {
deg[i]--;
if(deg[i] == 0) {
q.push(i);
}
}
}
}
// Print answer
if(works && order.size() == 26) {
cout << "DA" << endl;
order = convert(order);
cout << order << endl;
}
else {
cout << "NE" << endl;
}
}