#include<bits/stdc++.h>
using namespace std;
struct nodo{
char letra;
int max_prof;
vector<nodo*> listaAdy;
bool isLast;
nodo(char c){
letra = c;
max_prof = 0;
isLast = false;
}
};
struct nodo_smaller{
bool operator()(nodo* const &n1, nodo* const &n2) const {
return n1->max_prof < n2->max_prof;
}
};
//sort(V.begin(),V.end(), nodo_smaller{});
int construir(int pos, string& S, nodo* prev){
if(pos == S.size()) return 0;
nodo* act = NULL;
for(nodo* vecino: prev->listaAdy){
if(vecino->letra == S[pos]) act = vecino;
}
if(act == NULL){
act = new nodo(S[pos]);
prev->listaAdy.push_back(act);
}
act->max_prof = max(act->max_prof, 1 +construir(pos+1, S, act));
if (pos == S.size()-1) act->isLast = true;
return act->max_prof;
}
void type_printer (nodo* act, bool last){
if(act->letra != '.') cout << act->letra << '\n';
if (act->isLast){
cout << 'P' << '\n';
}
if(act->listaAdy.size() > 0){
sort(act->listaAdy.begin(), act->listaAdy.end(), nodo_smaller{});
for(int i = 0; i < act->listaAdy.size()-1; ++i){
type_printer(act->listaAdy[i], false);
}
type_printer(act->listaAdy[act->listaAdy.size()-1], last);
}
if(!last and (act->letra !='.')) cout << '-' << '\n';
}
int n_printer (nodo* act, bool last){
int ans = 0;
if(act->letra != '.') ans++;
if (act->isLast){
ans++;
}
if(act->listaAdy.size()> 0){
sort(act->listaAdy.begin(), act->listaAdy.end(), nodo_smaller{});
for(int i = 0; i < act->listaAdy.size()-1; ++i){
ans += n_printer(act->listaAdy[i], false);
}
ans += n_printer(act->listaAdy[act->listaAdy.size()-1], last);
}
if(!last and (act->letra !='.')) ans++;
return ans;
}
int main(){
int n;
string S;
cin >> n;
nodo inicio = nodo('.');
for(int i = 0; i < n; ++i){
cin >> S;
construir(0, S, &inicio);
}
cout << n_printer(&inicio, true) << '\n';
type_printer(&inicio, true);
return 0;
}
Compilation message
printer.cpp: In function 'int construir(int, std::string&, nodo*)':
printer.cpp:25:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if(pos == S.size()) return 0;
| ~~~~^~~~~~~~~~~
printer.cpp:35:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | if (pos == S.size()-1) act->isLast = true;
| ~~~~^~~~~~~~~~~~~
printer.cpp: In function 'void type_printer(nodo*, bool)':
printer.cpp:46:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nodo*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i = 0; i < act->listaAdy.size()-1; ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'int n_printer(nodo*, bool)':
printer.cpp:62:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nodo*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 0; i < act->listaAdy.size()-1; ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
724 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2260 KB |
Output is correct |
2 |
Correct |
16 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
5404 KB |
Output is correct |
2 |
Correct |
12 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
13140 KB |
Output is correct |
2 |
Correct |
106 ms |
30380 KB |
Output is correct |
3 |
Correct |
79 ms |
15628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
10104 KB |
Output is correct |
2 |
Correct |
136 ms |
36184 KB |
Output is correct |
3 |
Correct |
74 ms |
17776 KB |
Output is correct |
4 |
Correct |
114 ms |
34152 KB |
Output is correct |