#include <iostream>
#include <algorithm>
using namespace std;
struct TrieNode {
int mxdepth = 1;
bool word = false;
TrieNode *children[27] = {};
~TrieNode() {
delete[] children;
}
};
int n;
string s, pr;
TrieNode *root = new TrieNode, *now;
void calcmxdepth(TrieNode *node) {
for (int i = 0; i < 26; i++) {
if (!node->children[i]) continue;
calcmxdepth(node->children[i]);
if (node->children[i]->mxdepth >= node->mxdepth) {
node->mxdepth = node->children[i]->mxdepth + 1;
}
}
}
void print(TrieNode *node) {
if (!node) return;
if (node->word) pr.push_back('P');
short arr[26] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
sort(arr, arr + 26, [&](short a, short b) {
if (!node->children[a]) return false;
if (!node->children[b]) return true;
return node->children[a]->mxdepth < node->children[b]->mxdepth;
});
for (int i = 0; node->children[arr[i]]; i++) {
pr.push_back(arr[i] + 'a');
print(node->children[arr[i]]);
pr.push_back('-');
}
}
int main() {
cin >> n;
while (n--) {
cin >> s;
now = root;
for (char &x : s) {
if (!x) break;
if (!now->children[x - 'a']) {
now->children[x - 'a'] = new TrieNode;
}
now = now->children[x - 'a'];
}
now->word = true;
}
calcmxdepth(root);
print(root);
while (pr.back() == '-') pr.pop_back();
cout << pr.size() << '\n';
for (char &ch : pr) cout << ch << endl;
delete root;
return 0;
}
Compilation message
printer.cpp: In destructor 'TrieNode::~TrieNode()':
printer.cpp:9:18: warning: deleting array '((TrieNode*)this)->TrieNode::children'
9 | delete[] children;
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
340 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
688 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
19 ms |
3584 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
70 ms |
12492 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
25 ms |
30560 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
64 ms |
76744 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
58 ms |
60168 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |