#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<std::string> strings(n);
std::vector<std::vector<std::pair<char,int>>> trie(500001);
std::vector<int> subtreeSize(500001);
int curVertices = 0;
for(int i = 0; i < n; i++){
std::string s;
std::cin >> s;
int m = (int) s.length();
int curVertex = 0;
for(int j = 0; j < m; j++){
bool found = false;
for(std::pair<char,int> & p : trie[curVertex]){
if(p.first == s[j]){
found = true;
curVertex = p.second;
break;
}
}
if(found == false){
curVertices += 1;
trie[curVertex].push_back({s[j], curVertices});
curVertex = curVertices;
}
}
}
auto dfs = [&trie, &subtreeSize](int u, int p, auto && self) -> void {
subtreeSize[u] += 1;
for(std::pair<char,int> & v : trie[u]){
if(v.second != p){
self(v.second, u, self);
subtreeSize[u] += subtreeSize[v.second];
}
}
};
dfs(0, 0, dfs);
for(int i = 0; i <= curVertices; i++){
sort(trie[i].begin(), trie[i].end(), [&subtreeSize](const std::pair<char,int> & a, const std::pair<char,int> & b) -> bool {
return subtreeSize[a.second] < subtreeSize[b.second];
});
}
int built = 0;
std::vector<char> operations;
auto build = [&trie, &curVertices, &built, &operations](char c, int u, int p, auto && self) -> void{
if(u != 0){
operations.push_back(c);
built += 1;
}
if(trie[u].empty()){
operations.push_back('P');
}
for(std::pair<char,int> & v : trie[u]){
if(v.second != p){
self(v.first, v.second, u, self);
}
}
if(built < curVertices){
operations.push_back('-');
}
};
build('0', 0, 0, build);
std::cout << operations.size() << '\n';
for(char & c : operations){
std::cout << c << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13904 KB |
Output is correct |
2 |
Correct |
5 ms |
14160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14160 KB |
Output is correct |
2 |
Correct |
4 ms |
14160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
14160 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14160 KB |
Output is correct |
2 |
Incorrect |
6 ms |
13904 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
14160 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
14380 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15184 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
16844 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
20280 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
19404 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |