#include <bits/stdc++.h>
using namespace std;
struct Nodo
{
int fin;
int ma;
int hijo[26]; //Suponiendo que todas las letras solo son minusculas o solo mayusculas
int tams[26];
void iniciar()
{
ma=0;
fin=0;
for(int i=0; i<26; i++)
{
hijo[i]=-1; //Indica que no tiene un hijo en esa posicion
tams[i]=0;
}
}
};
const int largo = 5e5+1; //Largo maximo del trie
Nodo trie[largo];
int raiz = 0, tam = 1; //Raiz del trie y tamaño actual
void Agregar(string s)
{
int ta=s.size();
int nod = raiz; //El nodo actual va a ser raiz
for(auto c : s)
{
int x = c-'a'; //Saber su posicion en el array hijo
if(trie[nod].hijo[x]==-1)
{
trie[tam].iniciar();
trie[nod].hijo[x]=tam;
tam++;
}
trie[nod].tams[x]=max(trie[nod].tams[x],ta);
trie[nod].ma=max(trie[nod].ma,trie[nod].tams[x]);
nod = trie[nod].hijo[x];
ta--;
}
trie[nod].fin=1;
}
vector<char>ans;
void start(int nod)
{
if(trie[nod].fin)
ans.push_back('P');
int i;
vector<pair<int,int>>v;
for(i=0; i<26; i++)
{
if(trie[nod].hijo[i]!=-1)
{
v.push_back({trie[nod].tams[i],i});
}
}
sort(v.begin(),v.end());
for(i=0; i<int(v.size()); i++)
{
ans.push_back('a'+v[i].second);
start(trie[nod].hijo[v[i].second]);
}
ans.push_back('-');
}
int main()
{
trie[0].iniciar();
int n, i;
cin >> n;
string s;
for(i=0; i<n; i++)
{
cin >> s;
Agregar(s);
}
start(raiz);
while(ans.size()&&ans[int(ans.size())-1]=='-')
ans.pop_back();
cout << int(ans.size()) << '\n';
for(auto k:ans)
{
cout << k << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Output is correct |
2 |
Correct |
3 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6992 KB |
Output is correct |
2 |
Correct |
12 ms |
13392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
15308 KB |
Output is correct |
2 |
Correct |
7 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
36544 KB |
Output is correct |
2 |
Correct |
82 ms |
81084 KB |
Output is correct |
3 |
Correct |
48 ms |
43200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
28104 KB |
Output is correct |
2 |
Correct |
87 ms |
97724 KB |
Output is correct |
3 |
Correct |
48 ms |
47552 KB |
Output is correct |
4 |
Correct |
78 ms |
91836 KB |
Output is correct |