#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;
set<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]) {
assert(v != 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;
assert(size(st) <= m);
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); // inv perm is the original input
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++;
int a = words[i][k];
int b = words[j][k];
if (perm[i] < perm[j]) adj[b].insert(a);
else adj[a].insert(b);
}
}
FOR (i, 1, 27) adj[i].insert(0);
F0R (i, 27) {
if (!seen[i]) {
if (!dfs(i)) {
cout << "NE\n";
return 0;
}
}
}
assert(top[0] == 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;
dbg(top);
dbg(new_label);
// check answer
F0R (i, n) {
F0R (j, m) {
if (words[i][j] > 0) words[i][j] = new_label[words[i][j] - 1] + 1;
}
}
vt<int> ord(n);
iota(all(ord), 0);
sort(all(ord), [&] (int i, int j) { return words[i] < words[j]; });
vt<int> new_position(n);
F0R (i, n) new_position[ord[i]] = i;
F0R (i, n) {
assert(new_position[inv_perm[i]] == i);
}
}
Compilation message
cezar.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |