#include <bits/stdc++.h>
using namespace std;
const int SIGMA = 26;
struct node{
node* nxt[SIGMA];
bool word = false;
int heavy = -1;
node() {
for(int i = 0; i < SIGMA; i++){
nxt[i] = nullptr;
}
}
node* extend(int c){
if(!nxt[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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
1164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1876 KB |
Output is correct |
2 |
Correct |
24 ms |
2260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
6060 KB |
Output is correct |
2 |
Correct |
131 ms |
12676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
160 ms |
15044 KB |
Output is correct |
2 |
Correct |
48 ms |
3848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
404 ms |
37432 KB |
Output is correct |
2 |
Correct |
880 ms |
84644 KB |
Output is correct |
3 |
Correct |
460 ms |
44480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
29576 KB |
Output is correct |
2 |
Execution timed out |
1050 ms |
100768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |