Submission #748969

#TimeUsernameProblemLanguageResultExecution timeMemory
748969LucaLucaMType Printer (IOI08_printer)C++17
20 / 100
75 ms36592 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; bool term; node *f[26]; node() { cnt = term = 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 (const auto &ch : s) { if (curr -> f[ch - 'a'] == NULL) curr -> f[ch - 'a'] = new node; curr = curr -> f[ch - 'a']; curr -> cnt ++; } 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] -> cnt, 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; }
#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...