Submission #242876

#TimeUsernameProblemLanguageResultExecution timeMemory
242876aryan12Type Printer (IOI08_printer)C++17
100 / 100
234 ms64248 KiB
//https://oj.uz/problem/view/IOI08_printer #include <bits/stdc++.h> using namespace std; struct Node { bool leaf; int ending_strings; unordered_map<int, Node*> alphabet; Node() { leaf = false; ending_strings = 0; alphabet.clear(); } } *root; void trie_insert(string s) { Node *curr = root; for(int i = 0; i < s.size(); i++) { if(!curr->alphabet.count(s[i] - 'a')) { curr->alphabet[s[i] - 'a'] = new Node(); } curr = curr->alphabet[s[i] - 'a']; } curr->ending_strings++; curr->leaf = true; } string max_length = ""; void trie_Search(Node *curr, string s) { unordered_map<int, Node*> :: iterator itr; for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) { s += (char)(itr->first + 'a'); trie_Search(curr->alphabet[itr->first], s); s.pop_back(); } if(max_length.size() < s.size()) max_length = s; } char ans[1000010] = {0}; int currPos = 0; void FindAnswer(Node *curr, bool edge, int idx) { unordered_map<int, Node*> :: iterator itr; if(idx == max_length.size()) { for(int i = 0; i < curr->ending_strings; i++) { ans[currPos] = 'P'; currPos++; } return; } //cout << "Inside the loop" << endl; if(curr->leaf == true) { for(int i = 0; i < curr->ending_strings; i++) { ans[currPos] = 'P'; currPos++; } } for(itr = curr->alphabet.begin(); itr != curr->alphabet.end(); itr++) { //cout << i << " "; if(edge && itr->first == max_length[idx] - 'a') continue; ans[currPos] = (itr->first + 'a'); currPos++; FindAnswer(curr->alphabet[itr->first], false, idx + 1); ans[currPos] = '-'; currPos++; } //cout << "Have come out of the " << idx << " loop" << endl; if(edge) { int x = max_length[idx] - 'a'; ans[currPos] = max_length[idx]; currPos++; FindAnswer(curr->alphabet[x], true, idx + 1); } } void Solve() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 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 << currPos << "\n"; for(int i = 0; i < currPos; i++) { cout << (char)(ans[i]) << "\n"; } } int main() { Solve(); return 0; }

Compilation message (stderr)

printer.cpp: In function 'void trie_insert(std::__cxx11::string)':
printer.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < s.size(); i++) {
                    ~~^~~~~~~~~~
printer.cpp: In function 'void FindAnswer(Node*, bool, int)':
printer.cpp:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(idx == max_length.size()) {
        ~~~~^~~~~~~~~~~~~~~~~~~~
#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...