#include<bits/stdc++.h>
using namespace std;
vector<char> res;
struct Trie{
struct Node{
Node* child[26];
int cnt, exist;
Node(){
for(int i = 0; i < 26; i++) child[i] = NULL;
exist = cnt = 0;
}
};
int cur;
Node* root;
Trie() : cur(0){
root = new Node();
}
void add_string(string s){
Node* p = root;
for (auto f : s) {
int c = f - 'a';
if (p->child[c] == NULL) p->child[c] = new Node();
p = p->child[c];
p->cnt++;
}
p->exist++;
}
void Trie_Recursive(Node* p){
vector<pair<Node*, int>> tmp;
for(int i = 0; i < 26; i++){
if(p->child[i] != NULL){
tmp.push_back({p->child[i], i});
}
}
sort(tmp.begin(), tmp.end(), [](pair<Node*, int> x, pair<Node*, int> y){
if(x.first->cnt != y.first->cnt){
return x.first->cnt < y.first->cnt;
}
return x.second < y.second;
});
for(auto it : tmp){
res.push_back(char(it.second + 'a'));
Trie_Recursive(it.first);
res.push_back('-');
}
while(p->exist > 0){
res.push_back('P');
p->exist--;
}
}
};
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
Trie T;
for(int i = 0; i < n; i++){
string s; cin >> s;
T.add_string(s);
}
T.Trie_Recursive(T.root);
int cnt = 0;
vector<char> ans;
for(auto x : res){
if(cnt == n) break;
ans.push_back(x);
if(x == 'P') cnt++;
}
cout << ans.size() << endl;
for(auto x : ans) cout << x << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
15 ms |
1884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
54 ms |
6128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
138 ms |
14912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
329 ms |
37120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
298 ms |
28924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |