#include <iostream> #include <cstring> using namespace std; int main() { int n; string cur, temp, subs; long long sum = 0; cin>>n; cin>>cur; for (int i = 1; i < n; ++i) { cin>>temp; if (temp.size() < cur.size() || (temp.size() == cur.size() && temp <= cur)) { subs = cur.substr(0, temp.size()); if (temp < subs) { for (int j = 0; j < temp.size(); ++j) { cur[j] = temp[j]; } for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) { cur[j] = '0'; } for (int j = cur.size(); j >= max(temp.size(), cur.size() - 6); --j) { cur[j] = '0'; } cur += '0'; } else if (temp > subs) { for (int j = 0; j < temp.size(); ++j) { cur[j] = temp[j]; } for (int j = cur.size(); j >= max(temp.size(), cur.size() - 6); --j) { cur[j] = '0'; } for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) { cur[j] = '0'; } } else { if (cur.size() == temp.size()) { cur += '0'; } else { int q = cur.size() - 1; while (q >= temp.size() && cur[q] == '9') --q; if (q == temp.size() - 1) { // ? for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) cur[j] = 0; cur += '0'; } else { cur[q]++; for (int j = q + 1; j < cur.size(); ++j) { cur[j] = '0'; } } } } } else { cur = temp; } //cout<<cur<<' '<<temp<<' '<<cur.size() - temp.size()<<endl; sum += cur.size() - temp.size(); } cout<<sum<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include <cstring> using namespace std; int main() { int n; string cur, temp, subs; long long sum = 0; cin>>n; cin>>cur; for (int i = 1; i < n; ++i) { cin>>temp; if (temp.size() < cur.size() || (temp.size() == cur.size() && temp <= cur)) { subs = cur.substr(0, temp.size()); if (temp < subs) { for (int j = 0; j < temp.size(); ++j) { cur[j] = temp[j]; } for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) { cur[j] = '0'; } for (int j = cur.size(); j >= max(temp.size(), cur.size() - 6); --j) { cur[j] = '0'; } cur += '0'; } else if (temp > subs) { for (int j = 0; j < temp.size(); ++j) { cur[j] = temp[j]; } for (int j = cur.size(); j >= max(temp.size(), cur.size() - 6); --j) { cur[j] = '0'; } for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) { cur[j] = '0'; } } else { if (cur.size() == temp.size()) { cur += '0'; } else { int q = cur.size() - 1; while (q >= temp.size() && cur[q] == '9') --q; if (q == temp.size() - 1) { // ? for (int j = temp.size(); j < min(cur.size(), temp.size() + 6); ++j) cur[j] = 0; cur += '0'; } else { cur[q]++; for (int j = q + 1; j < cur.size(); ++j) { cur[j] = '0'; } } } } } else { cur = temp; } //cout<<cur<<' '<<temp<<' '<<cur.size() - temp.size()<<endl; sum += cur.size() - temp.size(); } cout<<sum<<endl; return 0; } |