#include <bits/stdc++.h>
using namespace std;
struct Node
{
Node *ch[26];
unsigned height = 0;
bool z;
};
void trie_insert(Node *x, string const &s, size_t i = 0)
{
if (i == s.size())
x->z = 1;
else
{
if (!x->ch[s[i] - 'a'])
{
x->ch[s[i] - 'a'] = (Node *)malloc(sizeof(Node));
memset(x->ch[s[i] - 'a']->ch, 0, sizeof x->ch[s[i] - 'a']->ch);
}
trie_insert(x->ch[s[i] - 'a'], s, i + 1);
x->height = x->ch[s[i] - 'a']->height + 1;
}
}
void trie_destroy(Node *x)
{
for (unsigned i = 0; i < 26; i++)
if (x->ch[i])
{
trie_destroy(x->ch[i]);
free(x->ch[i]);
}
}
void trie_dfs(Node *x, string &traversal)
{
if (x->z)
traversal.push_back('P');
vector<pair<unsigned, unsigned>> children;
for (unsigned i = 0; i < 26; i++)
{
if (x->ch[i])
{
children.emplace_back(x->ch[i]->height, i);
}
}
sort(children.begin(), children.end());
for (auto const &[ignore, i] : children)
{
if (x->ch[i])
{
traversal.push_back(i + 'a');
trie_dfs(x->ch[i], traversal);
traversal.push_back('-');
}
}
}
int main()
{
size_t n;
cin >> n;
Node root;
memset(root.ch, 0, sizeof root.ch);
for (size_t i = 0; i < n; i++)
{
string s;
cin >> s;
trie_insert(&root, s);
}
string traversal;
trie_dfs(&root, traversal);
trie_destroy(&root);
while (traversal.back() == '-')
traversal.pop_back();
cout << traversal.size() << '\n';
for (char const &c : traversal)
cout << c << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1712 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
5884 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
14756 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
36628 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
78 ms |
28620 KB |
printed invalid word |
2 |
Halted |
0 ms |
0 KB |
- |