제출 #555974

#제출 시각아이디문제언어결과실행 시간메모리
555974iDoItSaNdWiTcHType Printer (IOI08_printer)C++17
90 / 100
1080 ms99504 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1000000007; const ll INF = 1e9 + 5; ////////////////////////////////////////////////////////////////////////////////////////// string cur = ""; struct Trie { int totalsz = 0; bool isend = 0; struct Trie* children[26] = {}; }; void tinsert(struct Trie* root, string s) { //find the bug in tinsert Trie* cnt = root; for (int i = 0; i < s.length(); i++) { if (cnt->children[s[i] - 'a'] == nullptr) { //create new tree cnt->children[s[i] - 'a'] = new Trie(); } //calculate how much is left] int sz = cnt->children[s[i] - 'a']->totalsz; if (s.length() - i - 1 > sz) { (cnt->children[s[i] - 'a']->totalsz) = s.length() - i - 1; } cnt = cnt->children[s[i] - 'a']; } cnt->isend = 1; } void dfs(Trie* root) { //theres a string that ends here if (root->isend) { cur += "P"; } //first find the one with longest string int longestsz = 0; int c = 0; for (int i = 0; i < 26; i++) { if (root->children[i] != nullptr) { if (root->children[i]->totalsz > longestsz) { longestsz = root->children[i]->totalsz; c = i; } } } //do the others for (int i = 0;i < 26; i++) { if (root->children[i] != nullptr && i != c) { cur += char('a' + i); dfs(root->children[i]); cur += "-"; } } //last do back the longest string if (root->children[c] != nullptr) { cur += char('a' + c); dfs(root->children[c]); cur += "-"; } } void solve(){ int n; cin >> n; struct Trie* root = new Trie(); while (n--) { string s; cin >> s; tinsert(root, s); } dfs(root); int p = 0; for (int i = cur.length() - 1; i > -1; i--) { if (cur[i] == '-') p++; else break; } int maxx = cur.length() - p; cout << maxx << endl; for (int i = 0; i < maxx; i++) { cout << cur[i] << endl; } } ////////////////////////////////////////////////////////////////////////////////////////// int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; //cin >> t; while (t--){ solve(); } } /* find a method to dfs the longest strings at last */

컴파일 시 표준 에러 (stderr) 메시지

printer.cpp: In function 'void tinsert(Trie*, std::string)':
printer.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |  for (int i = 0; i < s.length(); i++) {
      |                  ~~^~~~~~~~~~~~
printer.cpp:29:26: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |   if (s.length() - i - 1 > sz) {
      |       ~~~~~~~~~~~~~~~~~~~^~~~
#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...