Submission #735273

#TimeUsernameProblemLanguageResultExecution timeMemory
735273PanosPaskType Printer (IOI08_printer)C++14
100 / 100
325 ms54144 KiB
#include <bits/stdc++.h> using namespace std; int n; queue<char> ans; struct Trie { int maxlength = 0; int size = 0; char maxkid = '\0'; bool isword = false; map<char, Trie> kids; void insert(const string& word, int pos) { size++; if (pos == word.size()) isword = true; else kids[word[pos]].insert(word, pos + 1); if (word.size() - pos + 1 > maxlength) { maxlength = word.size() - pos + 1; maxkid = word[pos]; } } void calculate_answer(bool leaveit) { if (isword) ans.push('P'); for (auto kid : kids) if (kid.first != maxkid && kid.second.size) { ans.push(kid.first); kid.second.calculate_answer(false); ans.push('-'); } if (maxkid != '\0') { ans.push(maxkid); kids[maxkid].calculate_answer(leaveit); if (!leaveit) ans.push('-'); } } }; Trie t; int main(void) { ios::sync_with_stdio(false); cin >> n; string s; for (int i = 0; i < n; i++) { cin >> s; t.insert(s, 0); } t.calculate_answer(true); cout << ans.size() << '\n'; while(!ans.empty()) { cout << ans.front() << '\n'; ans.pop(); } return 0; }

Compilation message (stderr)

printer.cpp: In member function 'void Trie::insert(const string&, int)':
printer.cpp:19:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |             (pos == word.size()) isword = true;
      |              ~~~~^~~~~~~~~~~~~~
printer.cpp:23:35: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   23 |         if (word.size() - pos + 1 > maxlength) {
      |             ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
#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...