Submission #1105321

#TimeUsernameProblemLanguageResultExecution timeMemory
1105321RareshikaType Printer (IOI08_printer)C++14
100 / 100
138 ms106632 KiB
#ifndef LOCAL #pragma GCC optimize("O3") #pragma GCC target("avx2") #endif #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #define pb push_back #define pii pair<int, int> using ll = long long; using ci = const int; using cll = const long long; using namespace std; /*******************************/ // INPUT / OUTPUT /*******************************/ /// GLOBAL DECLARATIONS int N; struct Trie { int cnt, fin, lant_maxim, lit_best = -1; Trie* fii[26]; }; Trie *root; vector <char> ops; /*******************************/ /// FUNCTIONS void ReadInput(); void Solution(); void Output(); /*******************************/ ///------------------------------------- inline void ReadInput() { cin >> N; } ///------------------------------------- inline Trie* adauga(Trie* trie, const string &cuv, int idx) { trie->cnt ++; if (idx == cuv.length()) { trie->fin ++; return trie; } // cerr << idx << " " << cuv[idx] - 'a' << "\n"; if (trie->fii[cuv[idx] - 'a'] == nullptr) trie->fii[cuv[idx] - 'a'] = new Trie(); trie->fii[cuv[idx] - 'a'] = adauga(trie->fii[cuv[idx] - 'a'], cuv, idx + 1); if (trie->lant_maxim < trie->fii[cuv[idx] - 'a']->lant_maxim + 1) { trie->lant_maxim = trie->fii[cuv[idx] - 'a']->lant_maxim + 1; trie->lit_best = cuv[idx] - 'a'; } return trie; } ///------------------------------------- inline void dfs(Trie* trie, bool nebun=false) { if (trie->fin > 0) ops.push_back('P'); for (int ch = 0 ; ch < 26 ; ++ ch) { if (ch == trie->lit_best) continue; if (trie->fii[ch] != nullptr && trie->fii[ch]->cnt > 0) { ops.push_back('a' + ch); dfs(trie->fii[ch]); } } if (trie->lit_best != -1) { ops.push_back('a' + trie->lit_best); dfs(trie->fii[trie->lit_best], nebun); } if (!nebun) ops.push_back('-'); } ///------------------------------------- inline void Solution() { root = new Trie(); string cuv; for (int i = 0 ; i < N ; ++ i) { cin >> cuv; root = adauga(root, cuv, 0); } dfs(root, true); } ///------------------------------------- inline void Output() { cout << ops.size() << "\n"; for (auto op: ops) cout << op << "\n"; } ///------------------------------------- int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #endif ReadInput(); Solution(); Output(); return 0; }

Compilation message (stderr)

printer.cpp: In function 'Trie* adauga(Trie*, const string&, int)':
printer.cpp:52:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     if (idx == cuv.length())
      |         ~~~~^~~~~~~~~~~~~~~
#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...