#include<bits/stdc++.h>
using namespace std;
vector<char> v;
struct trie
{
int dep, rasp;
trie *fii[27];
};
trie* init = new trie();
void add(trie *t, char *c)
{
if(*c == '\0')
{
t -> rasp++;
return;
}
if(t -> fii[*c - 'a'] == NULL)
t -> fii[*c - 'a'] = new trie();
add(t -> fii[*c - 'a'], c + 1);
}
void ans(trie *t)
{
for(int i = 1; i <= t -> rasp; i++)
v.push_back('P');
vector<pair<int, int>> ord;
for(int i = 0; i < 26; i++)
{
if(t -> fii[i] != NULL)
{
ord.push_back({t -> fii[i] -> dep, i});
}
}
sort(ord.begin(), ord.end());
for(auto u : ord)
{
v.push_back(char(u.second + 'a'));
ans(t -> fii[u.second]);
}
v.push_back('-');
}
void dfs_dep(trie *t)
{
t -> dep = 1;
for(int i = 0; i < 26; i++)
{
if(t -> fii[i] != NULL)
{
dfs_dep(t -> fii[i]);
t -> dep = max(t -> dep, (t -> fii[i] -> dep) + 1);
}
}
}
int main()
{
//freopen("trie.in", "r", stdin);
//freopen("trie.out", "w", stdout);
int n;
char y[27];
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> y;
add(init, y);
}
for(int i = 0; i < 26; i++)
if(init -> fii[i] != NULL)
dfs_dep(init -> fii[i]);
ans(init);
while(v.back() == '-')
v.pop_back();
cout << v.size() << "\n";
for(auto u : v)
cout << u << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
508 KB |
Output is correct |
2 |
Correct |
2 ms |
1204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2040 KB |
Output is correct |
2 |
Correct |
4 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6224 KB |
Output is correct |
2 |
Correct |
26 ms |
13388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
15812 KB |
Output is correct |
2 |
Correct |
10 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
38868 KB |
Output is correct |
2 |
Correct |
158 ms |
89536 KB |
Output is correct |
3 |
Correct |
88 ms |
46080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
30404 KB |
Output is correct |
2 |
Correct |
186 ms |
106436 KB |
Output is correct |
3 |
Correct |
95 ms |
52400 KB |
Output is correct |
4 |
Correct |
145 ms |
100548 KB |
Output is correct |