#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
os << "{";
int g = size(o);
for (auto i : o) os << i << ((--g) == 0 ? "" : ", ");
os << "}";
return os;
}
void _print() {
cerr << "\n";
}
template<typename T, typename ...V>
void _print(T t, V... v) {
cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);
}
#ifdef LOCAL
#define dbg(x...) cerr << #x << " = "; _print(x);
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
int n;
const int m = 100;
vt<vt<int>> words;
vt<int> perm;
vt<vt<int>> pfx_match;
vt<int> adj[27];
bool seen[27], in_stack[27];
vt<int> top;
bool dfs(int u) {
if (in_stack[u]) return false;
if (seen[u]) return true;
seen[u] = in_stack[u] = true;
bool res = true;
for (int v : adj[u]) {
res &= dfs(v);
}
top.pb(u);
in_stack[u] = false;
return res;
}
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
words.resize(n, vt<int>(m));
F0R (i, n) {
string st; cin >> st;
F0R (j, size(st)) words[i][j] = st[j] - 'a' + 1;
}
perm.resize(n);
vt<int> inv_perm(n);
F0R (i, n) cin >> perm[i], perm[i]--;
F0R (i, n) inv_perm[perm[i]] = i;
swap(perm, inv_perm);
pfx_match.resize(n, vt<int>(n));
F0R (i, n) pfx_match[i][i] = m;
F0R (i, n) {
FOR (j, i + 1, n) {
int k = 0;
while (words[i][k] == words[j][k]) k++;
pfx_match[i][j] = pfx_match[j][i] = k;
}
}
F0R (i, n) {
F0R (u, n) {
FOR (v, u + 1, n) {
if (pfx_match[u][v] == i) {
int a = words[u][i];
int b = words[v][i];
if (perm[u] < perm[v]) adj[b].pb(a);
else adj[a].pb(b);
}
}
}
}
FOR (i, 1, 27) adj[i].pb(0);
F0R (i, 27) {
if (!seen[i]) {
if (!dfs(i)) {
cout << "NE\n";
return 0;
}
}
}
cout << "DA\n";
vt<int> new_label(26);
FOR (i, 1, 27) {
new_label[top[i] - 1] = i - 1;
}
F0R (i, 26) {
cout << (char) (new_label[i] + 'a');
}
cout << endl;
}
Compilation message
cezar.cpp:69:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
69 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |