This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 110;
string s[N];
string st[N];
int cc[N];
vector <int> ed[30];
vector <char> ans;
char pos[30];
int visited[30];
void dfs(int v) {
visited[v] = true;
for (int u : ed[v]) {
if (!visited[u])
dfs(u);
}
ans.push_back(char(v + 'a'));
}
void topological_sort() {
ans.clear();
for (int i = 25; i >= 0; i--) {
if (visited[i] == 0) {
dfs(i);
}
}
reverse(ans.begin(), ans.end());
}
ll solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
}
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
st[i] = s[x];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
bool is = 0;
for (int k = 0; k < min(len(st[i]), len(st[j])); k++) {
if (st[i][k] != st[j][k]) {
is = 1;
ed[st[j][k] - 'a'].pb(st[i][k] - 'a');
break;
}
}
if (is == 0 && len(st[i]) < len(st[j])) {
cout << "NE\n";
return 0;
}
}
}
topological_sort();
if (sz(ans) < 26) {
cout << "NE\n";
return 0;
}
for (int i = 0; i < 26; i++) {
pos[ans[i] - 'a'] = char(i + 'a');
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < len(st[i]); j++) {
st[i][j] = pos[st[i][j] - 'a'];
}
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j < min(len(st[i]), len(st[i - 1])); j++) {
if (st[i][j] == st[i - 1][j])
continue;
if (st[i][j] < st[i - 1][j]) {
cout << "NE\n";
return 0;
} else {
break;
}
}
}
cout << "DA\n";
for (int i = 0; i < 26; i++)
cout << pos[i];
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |