답안 #692417

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
692417 2023-02-01T12:27:26 Z Litusiano Type Printer (IOI08_printer) C++14
100 / 100
195 ms 95424 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{
  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 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 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 1 ms 340 KB Output is correct
2 Correct 2 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1620 KB Output is correct
2 Correct 4 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 5520 KB Output is correct
2 Correct 29 ms 11596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 13516 KB Output is correct
2 Correct 9 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 34176 KB Output is correct
2 Correct 152 ms 80228 KB Output is correct
3 Correct 97 ms 40064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 25648 KB Output is correct
2 Correct 195 ms 95424 KB Output is correct
3 Correct 130 ms 45388 KB Output is correct
4 Correct 146 ms 90160 KB Output is correct