Submission #242711

#TimeUsernameProblemLanguageResultExecution timeMemory
242711aryan12Type Printer (IOI08_printer)C++17
20 / 100
471 ms193180 KiB
//https://oj.uz/problem/view/IOI08_printer #include <bits/stdc++.h> using namespace std; struct Node { bool leaf; int cnt_of_strings; unordered_map<int, Node*> alphabet; Node() { leaf = false; cnt_of_strings = 0; for(int i = 0; i < 26; i++) { alphabet[i] = NULL; } } } *root; void trie_insert(string s) { Node *curr = root; for(int i = 0; i < s.size(); i++) { if(curr->alphabet[s[i] - 'a'] == NULL) { curr->alphabet[s[i] - 'a'] = new Node(); } curr = curr->alphabet[s[i] - 'a']; curr->cnt_of_strings++; } curr->leaf = true; } string max_length = ""; void trie_Search(Node *curr, string s) { for(int i = 0; i < 26; i++) { if(curr->alphabet[i] != NULL) { s += (char)(i + 'a'); trie_Search(curr->alphabet[i], s); s.pop_back(); } } if(max_length.size() < s.size()) max_length = s; } string ans = ""; void FindAnswer(Node *curr, bool edge, int idx) { if(curr->leaf == true) { ans += "P"; return; } //cout << idx << endl; //cout << "Inside the loop" << endl; for(int i = 0; i < 26; i++) { //cout << i << " "; if(edge && i == max_length[idx] - 'a') continue; if(curr->alphabet[i] != NULL) { ans += (i + 'a'); FindAnswer(curr->alphabet[i], false, idx + 1); ans += "-"; } } //cout << "Have come out of the " << idx << " loop" << endl; if(edge) { int x = max_length[idx] - 'a'; ans += max_length[idx]; FindAnswer(curr->alphabet[x], true, idx + 1); } } void Solve() { int n; cin >> n; root = new Node(); for(int i = 1; i <= n; i++) { string s; cin >> s; trie_insert(s); } string s = ""; trie_Search(root, s); FindAnswer(root, true, 0); cout << ans.size() << endl; for(int i = 0; i < ans.size(); i++) { cout << (char)(ans[i]) << endl; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); Solve(); return 0; }

Compilation message (stderr)

printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:21:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i++) {
                    ~~^~~~~~~~~~
printer.cpp: In function 'void Solve()':
printer.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < ans.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...