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;
using ll = long long;
const int N = 500500;
int trie[N][26];
bool ch[N];
int mxd[N];
string ans = "";
void dfs_sz(int v) {
for (int i = 0;i < 26;i++) {
if (trie[v][i]) {
dfs_sz(trie[v][i]);
mxd[v] = max(mxd[v], mxd[trie[v][i]] + 1);
}
}
}
void dfs(int v, int keep) {
if (ch[v]) ans += "P";
if (keep) {
int big = -1;
for (int i = 0;i < 26;i++) {
if (trie[v][i]) {
if (mxd[trie[v][i]] == mxd[v] - 1) big = i;
}
}
for (int i = 0;i < 26;i++) {
if (trie[v][i] && trie[v][i] != trie[v][big]) {
ans += i + 'a';
dfs(trie[v][i], 0);
}
}
if (big != -1) {
ans += big + 'a';
dfs(trie[v][big], 1);
}
return;
}
for (int i = 0;i < 26;i++) {
if (trie[v][i]) {
ans += i + 'a';
dfs(trie[v][i], 0);
}
}
ans += "-";
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
int idx = 0;
for (int i = 1;i <= n;i++) {
string s;
cin >> s;
int now = 0;
for (auto& x : s) {
if (!trie[now][x - 'a'])
trie[now][x - 'a'] = ++idx;
now = trie[now][x - 'a'];
}
ch[now] = 1;
}
dfs_sz(0);
dfs(0, 1);
cout << ans.size() << '\n';
for (auto& x : ans) cout << x << '\n';
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... |