This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |