#include <bits/stdc++.h>
using namespace std;
struct Node
{
Node *ch[26];
unsigned height = 0;
bool z = 0;
};
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
1748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
5936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
14608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
36328 KB |
Output is correct |
2 |
Correct |
167 ms |
83704 KB |
Output is correct |
3 |
Incorrect |
97 ms |
43172 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
28384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |