#include <bits/stdc++.h>
using namespace std;
char ans[5000000];
struct arbol{
char letra;
int palabra_mayor;
bool fin_palabra=false;
int hijos[28];
};
int aux,desocupado=1;
arbol raices[500002];
int dfs(int posicion){
int tam=0;
for(int i=0;i<28;i++){
if(raices[posicion].hijos[i]!=0){
tam=max(dfs(raices[posicion].hijos[i]),tam);
}
}
tam++;
raices[posicion].palabra_mayor=tam;
return tam;
}
void res(int pos, bool rama){
if(pos!=0) {
ans[aux]=raices[pos].letra;
aux++;
}
if(raices[pos].fin_palabra==true) {
ans[aux]='P';
aux++;
}
int m=-1;
for(int i=0;i<28;i++){
if(raices[pos].hijos[i]!=0){
if(raices[raices[pos].hijos[i]].palabra_mayor==raices[pos].palabra_mayor-1&&m==-1) m=i;
else res(raices[pos].hijos[i],false);
}
}
if(pos==0&&m!=-1) res(raices[pos].hijos[m],true);
else if(m!=-1) res(raices[pos].hijos[m],rama);
if(pos!=0&&rama==false) {
ans[aux]='-';
aux++;
}
}
int main()
{
ios_base::sync_with_stdio();cin.tie(0);
int n;
cin>>n;
string palabra;
for(int i=0;i<n;i++){
cin>>palabra;
aux=0;
for(char l:palabra){
if(raices[aux].hijos[l-'a']!=0) aux=raices[aux].hijos[l-'a'];
else{
raices[aux].hijos[l-'a']=desocupado;
aux=desocupado;
raices[aux].letra=l;
desocupado++;
}
}
raices[aux].fin_palabra=true;
}
dfs(0);
aux=0;
res(0,false);
cout<<aux<<endl;
for(int i=0;i<aux;i++){
cout<<ans[i]<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
60876 KB |
Output is correct |
2 |
Correct |
24 ms |
60932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
60876 KB |
Output is correct |
2 |
Correct |
22 ms |
60900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
60876 KB |
Output is correct |
2 |
Correct |
27 ms |
60872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
60928 KB |
Output is correct |
2 |
Correct |
22 ms |
60900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
60876 KB |
Output is correct |
2 |
Correct |
31 ms |
60924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
60932 KB |
Output is correct |
2 |
Correct |
42 ms |
60976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
61056 KB |
Output is correct |
2 |
Correct |
160 ms |
61308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
61280 KB |
Output is correct |
2 |
Correct |
69 ms |
61124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
421 ms |
61948 KB |
Output is correct |
2 |
Correct |
899 ms |
63768 KB |
Output is correct |
3 |
Correct |
491 ms |
62416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
61916 KB |
Output is correct |
2 |
Execution timed out |
1049 ms |
64048 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |