#include <bits/stdc++.h>
using namespace std;
const int SIGMA = 26;
struct node{
map<int, node*> nxt;
bool word = false;
int heavy = -1;
node() {}
node* extend(int c){
if(!nxt.count(c)) nxt[c] = new node();
return nxt[c];
}
node* extend_heavy(int c){
heavy = c;
return nxt[c];
}
};
string ans;
void dfs(node *root){
if(root->word) ans.push_back('P');
for(int c = 0; c < SIGMA; c++){
if(root->nxt[c] && root->heavy != c){
ans.push_back('a' + c);
dfs(root->nxt[c]);
ans.push_back('-');
}
}
if(root->heavy != -1){
ans.push_back('a' + root->heavy);
dfs(root->nxt[root->heavy]);
ans.push_back('-');
}
}
node *root = new node();
int main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int n; cin >> n;
vector<string> vs;
for(int i = 0; i < n; i++){
string s; cin >> s;
vs.push_back(s);
node *x = root;
for(auto c : s){
x = x->extend(c - 'a');
}
x->word = true;
}
int idx = 0;
for(int i = 1; i < n; i++){
if(vs[i].size() > vs[idx].size()){
idx = i;
}
}
{
node *x = root;
for(auto c : vs[idx]){
x = x->extend_heavy(c - 'a');
}
}
dfs(root);
while(ans.back() == '-') ans.pop_back();
cout << ans.size() << endl;
for(auto c : ans) cout << c << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1236 KB |
Output is correct |
2 |
Correct |
20 ms |
6644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
11784 KB |
Output is correct |
2 |
Correct |
49 ms |
14816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
42468 KB |
Output is correct |
2 |
Correct |
250 ms |
91400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
107916 KB |
Output is correct |
2 |
Correct |
74 ms |
22260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
342 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
609 ms |
212060 KB |
Output is correct |
2 |
Runtime error |
333 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |