Submission #636143

#TimeUsernameProblemLanguageResultExecution timeMemory
636143tvladm2009Type Printer (IOI08_printer)C++14
100 / 100
214 ms105488 KiB
#include <iostream> using namespace std; const int SIGMA = 26; struct Trie { Trie *sons[SIGMA]; int freq; int words; bool is_ancestor; Trie() { freq = 0; words = 0; is_ancestor = false; for (int i = 0; i < SIGMA; i++) { sons[i] = NULL; } } }; void ins(Trie *&root, string s, int pos = 0) { if (pos == s.size()) { root->words++; } else { if (root->sons[s[pos] - 'a'] == NULL) { root->sons[s[pos] - 'a'] = new Trie(); } ins(root->sons[s[pos] - 'a'], s, pos + 1); } } int n, answer = 0; void stramosi(Trie *&root, string s, int pos = 0) { if (pos != s.size()) { root->sons[s[pos] - 'a']->is_ancestor = true; stramosi(root->sons[s[pos] - 'a'], s, pos + 1); } } void solve(Trie *&root) { answer += root->words; for (int i = 0; i < SIGMA; i++) { if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) { answer++; solve(root->sons[i]); answer++; } } for (int i = 0; i < SIGMA; i++) { if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) { answer++; solve(root->sons[i]); } } } void dfs(Trie *&root) { for (int i = 1; i <= root->words; i++) { cout << "P\n"; } for (int i = 0; i < SIGMA; i++) { if (root->sons[i] != NULL && root->sons[i]->is_ancestor == false) { cout << char(i + 'a') << "\n"; dfs(root->sons[i]); cout << "-\n"; } } for (int i = 0; i < SIGMA; i++) { if (root->sons[i] != NULL && root->sons[i]->is_ancestor == true) { cout << char(i + 'a') << "\n"; dfs(root->sons[i]); } } } int main() { cin >> n; Trie *root = new Trie(); string tmp = ""; for (int i = 1; i <= n; i++) { string s; cin >> s; if (s.size() > tmp.size()) { tmp = s; } ins(root, s); } stramosi(root, tmp); solve(root); cout << answer << "\n"; dfs(root); return 0; }

Compilation message (stderr)

printer.cpp: In function 'void ins(Trie*&, std::string, int)':
printer.cpp:24:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     if (pos == s.size()) {
      |         ~~~~^~~~~~~~~~~
printer.cpp: In function 'void stramosi(Trie*&, std::string, int)':
printer.cpp:37:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     if (pos != s.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...