This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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{
vector<pair<char,Node*>> son = vector<pair<char,Node*>>(26);
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(not Trie -> son[x[i]-'a'].s){
// creo el Node del Nou caracter,
Node* ne = new Node;
Trie-> son[x[i]-'a'].s = ne;
Trie-> son[x[i]-'a'].f = x[i];
}
add(Trie->son[x[i]-'a'].s,x,i+1);
}
int pc = 0;
void dfs(Node* u){
u-> sz = 0;
for(auto& x : u->son){
if(not x.s) continue;
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;
if(not a) return 0;
if(not b) return 1;
return a-> sz < b-> sz;
}
void dfs1(Node* u){
if(u->aca){
pc++; cout<<"P\n";
}
sort(all(u->son),cmp);
for(auto& x : u->son){
if(not x.s) continue;
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){
if(not x.s) continue;
//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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |