#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()){
| ~~^~~~~~~~~~~
# |
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 |
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 |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
11 ms |
1084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1724 KB |
Output is correct |
2 |
Correct |
26 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
5460 KB |
Output is correct |
2 |
Correct |
150 ms |
11656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
13524 KB |
Output is correct |
2 |
Correct |
39 ms |
3000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
416 ms |
34328 KB |
Output is correct |
2 |
Correct |
974 ms |
80128 KB |
Output is correct |
3 |
Correct |
516 ms |
40012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
25632 KB |
Output is correct |
2 |
Execution timed out |
1092 ms |
95228 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |