#include <bits/stdc++.h>
using namespace std;
const int NN = 1e6 + 10;
#define _for(i, a, b) for(int i = (a); i <= (b); ++i)
bool ED[NN], K[NN];
char L[NN];
int TR[NN][26], N, cnt, num;
string ans;
void insert(string &s){
int p = 0;
for(int i = 0; s[i]; ++i){
int x = s[i] - 'a';
if(!TR[p][x]) TR[p][x] = ++cnt;
L[TR[p][x]] = x + 'a', p = TR[p][x];
}
ED[p] = 1;
}
void mark(string &s){
int p = 0;
for(int i = 0; s[i]; i++){
int x = s[i] - 'a';
K[TR[p][x]] = 1, p = TR[p][x];
}
}
void dfs(int x){
if(ED[x] == 1 && x != 0) ++num, ans += "P";
if(num == N){
cout << ans.size() << endl;
for(int i = 0; ans[i]; ++i) cout << ans[i] << endl;
return;
}
int u;
_for(i, 0, 25){
u = TR[x][i];
if(K[u] == 0 && u != 0) ans += L[u], dfs(u), ans += "-";
}
_for(i, 0, 25){
u = TR[x][i];
if(K[u] && u) ans += L[u], dfs(u), ans += "-";
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> N;
string s, l;
_for(i, 1, N){
cin >> s, insert(s);
if(l.size() < s.size()) l = s;
}
mark(l), dfs(0);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
9 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3164 KB |
Output is correct |
2 |
Correct |
20 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
5216 KB |
Output is correct |
2 |
Correct |
128 ms |
8312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
9448 KB |
Output is correct |
2 |
Correct |
42 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
19900 KB |
Output is correct |
2 |
Correct |
837 ms |
42496 KB |
Output is correct |
3 |
Correct |
441 ms |
22920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
16108 KB |
Output is correct |
2 |
Correct |
998 ms |
50188 KB |
Output is correct |
3 |
Correct |
495 ms |
26100 KB |
Output is correct |
4 |
Correct |
961 ms |
47516 KB |
Output is correct |