#include <bits/stdc++.h>
using namespace std;
struct Node {
bool isLeaf;
vector<Node*> adj;
int dep, far;
Node(): isLeaf(false), adj(26, nullptr), dep(0), far(-1) {}
};
struct Trie {
Node* root;
Trie() { root = new Node; }
void insert(Node* &node, int pos, string &s) {
if (node == nullptr) node = new Node;
if (pos == (int) s.size()) {
node->isLeaf = true;
return;
}
insert(node->adj[s[pos] - 'a'], pos + 1, s);
node->dep = max(node->dep, node->adj[s[pos] - 'a']->dep + 1);
if (node->far == -1 || node->adj[s[pos] - 'a']->dep > node->adj[node->far]->dep) {
node->far = s[pos] - 'a';
}
}
void insert(string &s) {
insert(root, 0, s);
}
};
void dfs(Node* node, vector<char> &res) {
if (node->isLeaf) {
res.push_back('P');
}
for (int i = 0; i < 26; i++) {
if (node->adj[i] != nullptr && i != node->far) {
res.push_back((char)('a' + i));
dfs(node->adj[i], res);
res.push_back('-');
}
}
if (node->far != -1) {
res.push_back((char)('a' + node->far));
dfs(node->adj[node->far], res);
res.push_back('-');
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
Trie trie;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
trie.insert(s);
}
vector<char> res;
dfs(trie.root, res);
while (res.back() == '-') {
res.pop_back();
}
cout << res.size() << '\n';
for (auto &i : res) {
cout << i << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2124 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7116 KB |
Output is correct |
2 |
Correct |
20 ms |
15044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
17728 KB |
Output is correct |
2 |
Correct |
8 ms |
3928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
44120 KB |
Output is correct |
2 |
Correct |
124 ms |
101072 KB |
Output is correct |
3 |
Correct |
62 ms |
52016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
34512 KB |
Output is correct |
2 |
Correct |
136 ms |
120144 KB |
Output is correct |
3 |
Correct |
75 ms |
59068 KB |
Output is correct |
4 |
Correct |
127 ms |
113420 KB |
Output is correct |