#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Trie {
bool isEnd;
int depth;
vector<Trie*>link;
Trie(int dep): isEnd(false), depth(dep), link(vector<Trie*>(26, nullptr)) {}
void add(string &s) {
Trie *node = this;
int len = size(s);
for (char c : s) {
if (node->link[c - 'a']) {
node = node->link[c - 'a'];
} else {
node = node->link[c - 'a'] = new Trie(len);
}
}
node->isEnd = true;
}
string traverse(Trie *node) {
if (node->isEnd) return "P";
else {
string res = "";
int imaxDep = -1;
for (int i = 0; i < 26; ++i) {
if (node->link[i] && (imaxDep == -1 || node->link[i]->depth > node->link[imaxDep]->depth)) {
imaxDep = i;
}
}
for (int i = 0; i < 26; ++i) {
if (i != imaxDep && node->link[i]) {
res += ('a' + i);
res += traverse(node->link[i]);
res += '-';
}
}
res += ('a' + imaxDep);
res += traverse(node->link[imaxDep]);
res += '-';
return res;
}
}
};
void solve() {
int n;
cin >> n;
string s;
Trie *root = new Trie(0);
for (int i = 0; i < n; ++i) {
cin >> s;
root->add(s);
}
string res = root->traverse(root);
while (res.back() == '-') res.pop_back();
cout << size(res) << ln;
for (char c : res) {
cout << c << ln;
}
}
int main() {
ios_base::sync_with_stdio(false);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2120 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
6868 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
17236 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
43004 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
33484 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |