#include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <cstdint> using namespace std; int main() { cin.tie(0); std::ios_base::sync_with_stdio(false); int lt; cin>>lt; vector<string> words; words.reserve(lt); for(int i = 0; i < lt; ++i) { string s; cin>>s; words.push_back(std::move(s)); } int counter = 0; for(int i = 0; i < words.size()-1; ++i) { if (words[i].size() < words[i+1].size()) { continue; } else if(words[i].size() == words[i+1].size()) { if(words[i] < words[i+1]) { continue; } else { words[i+1] += "0"; ++counter; } } else { int iter = 0; while( (iter < words[i+1].size()) && (words[i][iter] == words[i+1][iter]) ) { ++iter; } if(iter < words[i+1].size()) { if(words[i][iter] < words[i+1][iter]) { auto w2s = words[i+1].size(); for(int j = 0; j < words[i].size()-w2s; ++j) { words[i+1] += "0"; ++counter; } } else { auto w2s = words[i+1].size(); for(int j = 0; j < words[i].size()-w2s+1; ++j) { words[i+1] += "0"; ++counter; } } } else { if(words[i][words[i].size()-1] != '9') { for(int j = words[i+1].size(); j < words[i].size()-1; ++j) { words[i+1] += words[i][j]; ++counter; } words[i+1] += (words[i][words[i].size()-1] + 1); ++counter; } else { int k = words[i].size()-1; for(;k > words[i+1].size()-1; --k) { if(words[i][k] != '9') { break; } } if(k > words[i+1].size()-1) { for(int m = words[i+1].size(); m < k; ++m) { words[i+1] += words[i][m]; ++counter; } words[i+1] += (words[i][k]+1); ++counter; for(int n = k+1; n < words[i].size(); ++n) { words[i+1] += "0"; ++counter; } } else { auto w2s = words[i+1].size(); for(int m = 0; m < words[i].size() - w2s + 2; ++m) { words[i+1] += "0"; ++counter; } } } } } } cout<<counter<<endl; }
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | #include <iostream> #include <string> #include <vector> #include <algorithm> #include <utility> #include <cstdint> using namespace std; int main() { cin.tie(0); std::ios_base::sync_with_stdio(false); int lt; cin>>lt; vector<string> words; words.reserve(lt); for(int i = 0; i < lt; ++i) { string s; cin>>s; words.push_back(std::move(s)); } int counter = 0; for(int i = 0; i < words.size()-1; ++i) { if (words[i].size() < words[i+1].size()) { continue; } else if(words[i].size() == words[i+1].size()) { if(words[i] < words[i+1]) { continue; } else { words[i+1] += "0"; ++counter; } } else { int iter = 0; while( (iter < words[i+1].size()) && (words[i][iter] == words[i+1][iter]) ) { ++iter; } if(iter < words[i+1].size()) { if(words[i][iter] < words[i+1][iter]) { auto w2s = words[i+1].size(); for(int j = 0; j < words[i].size()-w2s; ++j) { words[i+1] += "0"; ++counter; } } else { auto w2s = words[i+1].size(); for(int j = 0; j < words[i].size()-w2s+1; ++j) { words[i+1] += "0"; ++counter; } } } else { if(words[i][words[i].size()-1] != '9') { for(int j = words[i+1].size(); j < words[i].size()-1; ++j) { words[i+1] += words[i][j]; ++counter; } words[i+1] += (words[i][words[i].size()-1] + 1); ++counter; } else { int k = words[i].size()-1; for(;k > words[i+1].size()-1; --k) { if(words[i][k] != '9') { break; } } if(k > words[i+1].size()-1) { for(int m = words[i+1].size(); m < k; ++m) { words[i+1] += words[i][m]; ++counter; } words[i+1] += (words[i][k]+1); ++counter; for(int n = k+1; n < words[i].size(); ++n) { words[i+1] += "0"; ++counter; } } else { auto w2s = words[i+1].size(); for(int m = 0; m < words[i].size() - w2s + 2; ++m) { words[i+1] += "0"; ++counter; } } } } } } cout<<counter<<endl; } |