Submission #231585

#TimeUsernameProblemLanguageResultExecution timeMemory
231585AlexLuchianovType Printer (IOI08_printer)C++14
100 / 100
239 ms99692 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...