답안 #692415

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
692415 2023-02-01T12:25:14 Z Litusiano Type Printer (IOI08_printer) C++14
100 / 100
301 ms 208320 KB
#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

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 1 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 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 4 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3412 KB Output is correct
2 Correct 6 ms 4308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 11992 KB Output is correct
2 Correct 49 ms 25748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 30348 KB Output is correct
2 Correct 16 ms 6484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 76180 KB Output is correct
2 Correct 256 ms 175052 KB Output is correct
3 Correct 151 ms 89736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 59376 KB Output is correct
2 Correct 301 ms 208320 KB Output is correct
3 Correct 146 ms 101976 KB Output is correct
4 Correct 274 ms 196520 KB Output is correct