#include <bits/stdc++.h>
using namespace std;
char ans[5000000];
struct arbol{
char letra;
int opcion;
bool fin_palabra=false;
int hijos[28];
};
string mayor;
int aux,desocupado=1,valor=1;
arbol raices[500002];
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]].opcion==valor) {
// cout<<pos<<endl;
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;
if(palabra.size()>mayor.size()) valor++;
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++;
}
if(mayor.size()<palabra.size()) raices[aux].opcion=valor;
}
if(mayor.size()<palabra.size()) mayor=palabra;
raices[aux].fin_palabra=true;
}
desocupado=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 |
22 ms |
60868 KB |
Output is correct |
2 |
Correct |
28 ms |
60848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
60892 KB |
Output is correct |
2 |
Correct |
22 ms |
60932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
60932 KB |
Output is correct |
2 |
Correct |
26 ms |
60948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
60864 KB |
Output is correct |
2 |
Correct |
22 ms |
60876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
60952 KB |
Output is correct |
2 |
Correct |
30 ms |
60968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
60996 KB |
Output is correct |
2 |
Correct |
44 ms |
61004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
61048 KB |
Output is correct |
2 |
Correct |
176 ms |
61340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
181 ms |
61240 KB |
Output is correct |
2 |
Correct |
74 ms |
61232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
406 ms |
61864 KB |
Output is correct |
2 |
Correct |
874 ms |
63568 KB |
Output is correct |
3 |
Correct |
496 ms |
62432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
343 ms |
61712 KB |
Output is correct |
2 |
Execution timed out |
1073 ms |
63904 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |