Submission #114522

#TimeUsernameProblemLanguageResultExecution timeMemory
114522popovicirobertType Printer (IOI08_printer)C++14
100 / 100
525 ms34708 KiB
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long // 217 // 44 using namespace std; const int INF = (int) 1e9; const int B = 41; inline int get(string &a, string &b) { int p = 0; while(p < min(a.size(), b.size()) && a[p] == b[p]) { p++; } return p; } map <ull, int> mp; pair <int, int> solve(vector <string> &str, ull hsh) { int n = str.size(); sort(str.begin(), str.end()); vector < pair <int, int> > fr(26); for(auto &it : fr) { it.first = it.second = INF; } pair <int, int> ans = {0, INF}; int i = 0; while(i < n) { vector <string> aux; aux.clear(); int j = i; while(j < n && str[i][0] == str[j][0]) { if(str[j].size() > 1) { aux.push_back(str[j].substr(1, (int) str[j].size() - 1)); } j++; } char ch = str[i][0] - 'a'; if(aux.size()) { fr[ch] = solve(aux, hsh * B + ch + 1); fr[ch].first += 2; fr[ch].second++; } else { fr[ch] = {2, 1}; } i = j; } int sum = 0; for(i = 0; i < 26; i++) { if(fr[i].first != INF) { sum += fr[i].first; } } ans.first = sum; for(i = 0; i < 26; i++) { if(fr[i].second == INF) { continue; } int cur = sum - fr[i].first + fr[i].second; if(cur < ans.second) { ans.second = cur; mp[hsh] = i; } } return ans; } void gen(vector <string> &str, ull hsh) { int n = str.size(); char ch = mp[hsh] + 'a'; sort(str.begin(), str.end()); vector <string> sol, aux; int p = 0; for(int i = 0; i < n; i++) { if(str[i][0] != ch) { sol.push_back(str[i]); } else { aux.push_back({}); if(str[i].size() > 1) { aux[p++] = str[i].substr(1, (int) str[i].size() - 1); } } } if(p > 0) { gen(aux, hsh * B + ch - 'a' + 1); } for(auto &it : aux) { sol.push_back(ch + it); } swap(str, sol); } inline void tr(string &a, string &b) { int p = 0; while(p < min(a.size(), b.size()) && a[p] == b[p]) { p++; } while(a.size() > p) { cout << "-\n"; a.pop_back(); } while(p < b.size()) { cout << b[p] << "\n"; p++; } cout << "P\n"; a = b; } int main() { //ifstream cin("A.in"); //ofstream cout("A.out"); int i, n; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; vector <string> str(n); for(i = 0; i < n; i++) { cin >> str[i]; } cout << solve(str, 0).second + n << "\n"; gen(str, 0); string last; for(auto it : str) { tr(last, it); } //cin.close(); //cout.close(); return 0; }

Compilation message (stderr)

printer.cpp: In function 'int get(std::__cxx11::string&, std::__cxx11::string&)':
printer.cpp:15:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < min(a.size(), b.size()) && a[p] == b[p]) {
           ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'void tr(std::__cxx11::string&, std::__cxx11::string&)':
printer.cpp:121:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < min(a.size(), b.size()) && a[p] == b[p]) {
           ~~^~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp:124:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(a.size() > p) {
           ~~~~~~~~~^~~
printer.cpp:128:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(p < b.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...