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;
int main() {
cin.tie(0)->sync_with_stdio(0);
struct node {
node* s[26] = {};
char letter;
int end = 0;
node() {}
node(char x) : letter(x) {}
} *root = new node;
auto insert = [&](const string& s) {
node* cur = root;
for (auto x : s) {
if (!cur->s[x - 'a'])
cur->s[x - 'a'] = new node(x);
cur = cur->s[x - 'a'];
}
cur->end = 1;
};
vector<char> sol;
function<void(node*)> dfs = [&](node* nod) {
if (nod->end)
sol.emplace_back('P');
for (int i = 0; i < 26; ++i)
if (nod->s[i]) {
sol.emplace_back(nod->s[i]->letter);
dfs(nod->s[i]);
sol.emplace_back('-');
}
};
int n;
string maxs;
cin >> n;
while (n--) {
string s;
cin >> s;
insert(s);
if (s.length() > maxs.length())
maxs = s;
}
node* cur = root;
for (auto x : maxs) {
swap(cur->s[25], cur->s[x - 'a']);
cur = cur->s[25];
}
dfs(root);
while (sol.back() == '-')
sol.pop_back();
cout << sol.size() << '\n';
for (auto x : sol)
cout << x << '\n';
}
# | 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... |