Submission #231585

# Submission time Handle Problem Language Result Execution time Memory
231585 2020-05-14T05:17:31 Z AlexLuchianov Type Printer (IOI08_printer) C++14
100 / 100
239 ms 99692 KB
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>

using namespace std;

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 25000;
int const sigma = 26;

vector<char> sol;

class Trie{
private:
  struct Node{
    int _scount;
    int depth;
    Node* son[sigma];
    Node(){
      _scount = 0;
      for(int i = 0; i < sigma; i++)
        son[i] = nullptr;
    }
  };
public:
  void _insert(Node* &root, string &s, int ptr){
    if(root == nullptr)
      root = new Node();
    if(s.size() == ptr)
      root->_scount++;
    else
      _insert(root->son[s[ptr] - 'a'], s, ptr + 1);
  }

  void dfs(Node* &root){
    if(root == nullptr)
      return ;
    root->depth = 1;
    for(int h = 0; h < sigma; h++)
      if(root->son[h] != nullptr) {
        dfs(root->son[h]);
        root->depth = max(root->depth, root->son[h]->depth + 1);
      }
  }
  void print(Node* &root, bool discount){
    if(root == nullptr)
      return ;
    for(int h = 0; h < root->_scount; h++)
      sol.push_back('P');

    int chosen = 0;
    for(int h = 0; h < sigma; h++) {
      if(root->son[h] == nullptr)
        continue;
      if(root->son[h]->depth + 1 == root->depth)
        chosen = h;
    }
    if(discount == 0)
      chosen = -1;

    for(int h = 0; h < sigma; h++){
      if(h == chosen)
        continue;
      if(root->son[h] != nullptr){
        sol.push_back(char(h + 'a'));
        print(root->son[h], 0);
        sol.push_back('-');
      }
    }
    if(-1 != chosen && root->son[chosen] != nullptr){
      sol.push_back(char(chosen + 'a'));
      print(root->son[chosen], 1);
    }
  }
  Node* root = nullptr;
};

int main()
{
  int n;
  cin >> n;
  Trie trie;
  for(int i = 1;i <= n; i++){
    string s;
    cin >> s;
    trie._insert(trie.root, s, 0);
  }
  trie.dfs(trie.root);
  trie.print(trie.root, 1);
  cout << sol.size() << '\n';
  for(int i = 0; i < sol.size(); i++)
    cout << sol[i] << '\n';
  return 0;
}

Compilation message

printer.cpp: In member function 'void Trie::_insert(Trie::Node*&, std::__cxx11::string&, int)':
printer.cpp:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(s.size() == ptr)
        ~~~~~~~~~^~~~~~
printer.cpp: In function 'int main()':
printer.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < sol.size(); i++)
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 7 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1920 KB Output is correct
2 Correct 9 ms 2304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 6016 KB Output is correct
2 Correct 35 ms 12536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 14840 KB Output is correct
2 Correct 21 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 36724 KB Output is correct
2 Correct 204 ms 83948 KB Output is correct
3 Correct 126 ms 43248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 28784 KB Output is correct
2 Correct 239 ms 99692 KB Output is correct
3 Correct 139 ms 49140 KB Output is correct
4 Correct 220 ms 94188 KB Output is correct