#include <bits/stdc++.h>
using namespace std;
vector<char> ans;
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.push_back(raices[pos].letra);
}
if(raices[pos].fin_palabra==true) {
ans.push_back('P');
}
int m=0;
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==0) m=i;
else res(raices[pos].hijos[i],false);
}
}
if(pos==0&&m!=0) res(raices[pos].hijos[m],true);
else if(m!=0) res(raices[pos].hijos[m],rama);
if(pos!=0&&rama==false) {
ans.push_back('-');
}
}
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);
res(0,false);
cout<<ans.size()<<endl;
for(char r:ans){
cout<<r<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
60908 KB |
Output is correct |
2 |
Incorrect |
21 ms |
60872 KB |
didn't print every word |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
60876 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
60920 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
60876 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
60908 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
60984 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
89 ms |
61072 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
184 ms |
61328 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
340 ms |
61800 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
327 ms |
61876 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |