이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |