#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define f first
#define s second
#define ii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define vvii vector<vector<ii>>
#define pb push_back
#define vpi vector<ii>
#define forcin for(int i = 0; i<n; ++i) cin>>v[i];
#define pq priority_queue<ii>
#define mp make_pair
#define ld long double
#define vc vector<char>
#define vvc vector<vc>
#define vb vector<bool>
#define vvb vector<vb>
#define all(a) (a).begin(),(a).end()
#define For(i, n, x) for (int i = x; i < n; i++)
#define rsz(a,x) assign(a,x)
#define endl "\n"
int n;
struct Node{
unordered_map<char,Node*> son;
bool aca = 0;
int sz = 0;
int sub = 0;
};
void add(Node* Trie, string& x, int i){
if(i == x.size()){
Trie -> aca = 1;
return;
}
if(Trie -> son.find(x[i]) == Trie -> son.end()){
// creo el Node del Nou caracter,
Node* ne = new Node;
Trie-> son[x[i]] = ne;
}
add(Trie->son[x[i]],x,i+1);
}
int pc = 0;
void dfs(Node* u){
u-> sz = 0;
for(auto& x : u->son){
dfs(x.s);
u->sz = max(u->sz, x.s->sz);
u-> sub += x.s->sub;
}
u->sz++;
u-> sub++;
}
bool cmp(pair<char,Node*>& c, pair<char,Node*>& d){
Node* a = c.s; Node* b = d.s;
return a-> sz < b-> sz;
}
void dfs1(Node* u){
if(u->aca){
pc++; cout<<"P\n";
}
vector<pair<char,Node*>> v;
for(auto x : u->son){
v.pb(x);
}
sort(all(v),cmp);
for(auto& x : v){
cout<<x.f<<endl;
dfs1(x.s);
if(pc != n) cout<<'-'<<endl;;
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
Node* st = new Node;
For(i,n,0){
string x; cin>>x;
add(st,x,0);
}
// Exploro la Trie
int s = 0; int mx = 0;
dfs(st);
for(auto& x : st->son){
//cerr<<x.f<<" "<<x.s->sub<<endl;
s+=x.s->sub;
mx = max(mx,x.s->sz);
}
cout<<s*2-mx+n<<endl;
dfs1(st);
}
Compilation message
printer.cpp: In function 'void add(Node*, std::string&, int)':
printer.cpp:36:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if(i == x.size()){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1620 KB |
Output is correct |
2 |
Correct |
3 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5460 KB |
Output is correct |
2 |
Correct |
26 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
13516 KB |
Output is correct |
2 |
Correct |
10 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
34152 KB |
Output is correct |
2 |
Correct |
140 ms |
80188 KB |
Output is correct |
3 |
Correct |
85 ms |
39904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
25616 KB |
Output is correct |
2 |
Correct |
183 ms |
95320 KB |
Output is correct |
3 |
Correct |
91 ms |
45388 KB |
Output is correct |
4 |
Correct |
149 ms |
89912 KB |
Output is correct |