#include <bits/stdc++.h>
using namespace std;
vector<char> ans;
struct arbol{
char letra;
int palabra_mayor;
bool fin_palabra=false;
vector<int> hijos;
};
int aux,desocupado=1;
arbol raices[500002];
int dfs(int posicion){
int tam=0;
for(int i:raices[posicion].hijos){
tam=max(dfs(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=-1;
for(int i:raices[pos].hijos){
if(raices[i].palabra_mayor==raices[pos].palabra_mayor-1&&m==-1) m=i;
else res(i,false);
}
if(pos==0&&m!=-1) res(m,true);
else if(m!=-1) res(m,rama);
if(pos!=0&&rama==false) {
ans.push_back('-');
}
}
int main()
{
ios_base::sync_with_stdio();cin.tie(0);
int n;
cin>>n;
bool c=false;
for(int i=0;i<n;i++){
string palabra;
cin>>palabra;
aux=0;
for(char l:palabra){
c=false;
for(int j:raices[aux].hijos){
if(raices[j].letra==l) {
c=true;
aux=j;
break;
}
}
if(c==false){
raices[aux].hijos.push_back(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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19788 KB |
Output is correct |
2 |
Correct |
10 ms |
19788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19800 KB |
Output is correct |
2 |
Correct |
9 ms |
19808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
19848 KB |
Output is correct |
2 |
Correct |
10 ms |
19788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19856 KB |
Output is correct |
2 |
Correct |
10 ms |
19788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19840 KB |
Output is correct |
2 |
Correct |
19 ms |
19916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
20104 KB |
Output is correct |
2 |
Correct |
32 ms |
20128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
20852 KB |
Output is correct |
2 |
Correct |
164 ms |
21912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
22080 KB |
Output is correct |
2 |
Correct |
58 ms |
20344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
407 ms |
25536 KB |
Output is correct |
2 |
Correct |
900 ms |
33104 KB |
Output is correct |
3 |
Correct |
474 ms |
26488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
340 ms |
24004 KB |
Output is correct |
2 |
Execution timed out |
1076 ms |
35584 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |