#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;
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]) {
assert(v != u);
res &= dfs(v);
}
top.pb(u);
in_stack[u] = false;
return res;
}
bool check(vt<string> words, string key, vt<int> perm) {
// check key
assert(size(key) == 26);
set<char> pool;
for (char c : key) pool.insert(c);
assert(size(pool) == size(key));
for (string& st : words) {
for (char& c : st) {
c = key[c - 'a'];
}
}
auto sorted = words;
sort(all(sorted));
int n = size(perm);
F0R (i, n) {
string w = words[perm[i]];
auto it = find(all(sorted), w);
if (it - sorted.begin() != i) return false;
}
return true;
}
vt<string> original_words;
string key;
vt<int> original_perm;
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
words.resize(n, vt<int>(m));
original_words.resize(n);
F0R (i, n) {
string st; cin >> st;
original_words[i] = 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]--;
original_perm = perm;
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].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";
volatile unsigned long long bruh = 1294;
F0R (i, 1e8) {
bruh *= 1241231;
}
return 0;
}
}
}
assert(top[0] == 0);
vt<int> new_label(26);
FOR (i, 1, 27) {
new_label[top[i] - 1] = i - 1;
}
F0R (i, 26) {
assert('a' <= new_label[i] + 'a' && new_label[i] + 'a' <= 'z');
key += (new_label[i] + 'a');
}
assert(check(original_words, key, original_perm));
cout << "DA\n";
cout << key << endl;
}
Compilation message
cezar.cpp:75:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
75 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
93 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
98 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
98 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
98 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
99 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
98 ms |
572 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 |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |