Submission #748991

#TimeUsernameProblemLanguageResultExecution timeMemory
748991LucaLucaMType Printer (IOI08_printer)C++17
100 / 100
186 ms106480 KiB
#include <bits/stdc++.h> using namespace std; /** Initial costul e suma lungimilor. Punem fiecare string in trie Cand coboram in trie daca trb sa afisam, afisam Cand urcam in trie, dam delete **/ vector<char>ans; struct node { int cnt, dep; bool term; node *f[26]; node() { cnt = term = dep = 0; memset(f, 0, sizeof(f)); } }; struct Trie { private: node *root = new node; public: Trie(){} node *getRoot () { return root; } void add (const string &s) { node *curr = root; for (int i=0; i<(int)s.size(); i++) { if (curr -> f[s[i] - 'a'] == NULL) curr -> f[s[i] - 'a'] = new node; curr = curr -> f[s[i] - 'a']; curr -> cnt ++; curr -> dep = max(curr -> dep, (int)s.size() - i); } curr -> term = true; } void dfs (node *curr) { if (curr -> term == true) ans.push_back('P'); vector<pair<int, int>>v; for (int i=0; i<26; i++) { if (curr -> f[i] != NULL) v.push_back({curr -> f[i] -> dep, i}); } sort(v.begin(), v.end()); for (int i=0; i<(int)v.size(); i++) { ans.push_back(char(v[i].second + 'a')); dfs(curr -> f[v[i].second]); } ans.push_back('-'); } }; int main() { int n; cin >> n; Trie T; for (int i=1; i<=n; i++) { string s; cin >> s; T.add(s); } T.dfs(T.getRoot()); while (!ans.empty() && ans.back() == '-') ans.pop_back(); cout << (int)ans.size() << '\n'; for (const auto &ch : ans) cout << ch << '\n'; return 0; }

Compilation message (stderr)

printer.cpp: In constructor 'node::node()':
printer.cpp:21:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   21 |         cnt = term = dep = 0;
      |                      ~~~~^~~
#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...