#include <stdio.h> #include <iostream> #include <string> bool is_equal(const std::string s1, const std::string s2) { return s1.compare(s2) == 0; } bool __is_lower(const char* s1, const char* s2, long long len) { for(long long i = 0; i < len; i++) { if(s1[i] > s2[i]) { return false; } } if(s1[len -1] >= s2[len -1]) { return false; } return true; } bool is_lower(const std::string s1, const std::string s2) { long long l1 = s1.length(); long long l2 = s2.length(); if(l1 == l2) { return __is_lower(s1.c_str(), s2.c_str(), l1); } else if (l1 < l2) { return true; } return false; } long long process_case1(std::string& s1, std::string& s2) { long long l1 = s1.length(); long long l2 = s2.length(); long long ldiff = l1 -l2; if(l1 == l2) { s2 += '0'; return 1; } else if(l1 > l2) { size_t found = s1.find(s2); if(found !=0) { long long res = 0; do { s2+= '0'; res++; } while(!is_lower(s1,s2)); return res; } else { int i = 1; bool gotit =false; for( i=1; i<=ldiff; i++) { char c = s1.c_str()[l1-i]; if(c < '9') { s2.clear(); s2.append(s1,0,l1-i); s2 += (c+1); gotit=true; break; } } if(gotit==true) { long long to_cover = s1.length() -s2.length(); for(int j =0; j<to_cover; j++) { s2 += '0'; } return ldiff; } else { s2.clear(); s2.append(s1); s2 += '0'; return ldiff+1; } } } return 0; } long long process_two(std::string& s1, std::string& s2) { if(is_lower(s1, s2)) { return 0; } else { return process_case1(s1,s2); } } std::istream& get_next_lower(std::istream& inpuci) { } long long get_result(std::istream& input) { long long n; input >> n; std::string s1, s2; input >> s1; if(n == 1) { return 0; } else { long long result = 0; for(long long i = 1; i < n; i++) { input >> s2; result += process_two(s1,s2); s1.clear(); s1.append(s2); s2.clear(); } return result; } } #ifndef TEST int main(int argc, char* argv[]) { #else int _main(int argc, char* argv[]) { #endif printf("%lld\n", get_result(std::cin)); }
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 | #include <stdio.h> #include <iostream> #include <string> bool is_equal(const std::string s1, const std::string s2) { return s1.compare(s2) == 0; } bool __is_lower(const char* s1, const char* s2, long long len) { for(long long i = 0; i < len; i++) { if(s1[i] > s2[i]) { return false; } } if(s1[len -1] >= s2[len -1]) { return false; } return true; } bool is_lower(const std::string s1, const std::string s2) { long long l1 = s1.length(); long long l2 = s2.length(); if(l1 == l2) { return __is_lower(s1.c_str(), s2.c_str(), l1); } else if (l1 < l2) { return true; } return false; } long long process_case1(std::string& s1, std::string& s2) { long long l1 = s1.length(); long long l2 = s2.length(); long long ldiff = l1 -l2; if(l1 == l2) { s2 += '0'; return 1; } else if(l1 > l2) { size_t found = s1.find(s2); if(found !=0) { long long res = 0; do { s2+= '0'; res++; } while(!is_lower(s1,s2)); return res; } else { int i = 1; bool gotit =false; for( i=1; i<=ldiff; i++) { char c = s1.c_str()[l1-i]; if(c < '9') { s2.clear(); s2.append(s1,0,l1-i); s2 += (c+1); gotit=true; break; } } if(gotit==true) { long long to_cover = s1.length() -s2.length(); for(int j =0; j<to_cover; j++) { s2 += '0'; } return ldiff; } else { s2.clear(); s2.append(s1); s2 += '0'; return ldiff+1; } } } return 0; } long long process_two(std::string& s1, std::string& s2) { if(is_lower(s1, s2)) { return 0; } else { return process_case1(s1,s2); } } std::istream& get_next_lower(std::istream& inpuci) { } long long get_result(std::istream& input) { long long n; input >> n; std::string s1, s2; input >> s1; if(n == 1) { return 0; } else { long long result = 0; for(long long i = 1; i < n; i++) { input >> s2; result += process_two(s1,s2); s1.clear(); s1.append(s2); s2.clear(); } return result; } } #ifndef TEST int main(int argc, char* argv[]) { #else int _main(int argc, char* argv[]) { #endif printf("%lld\n", get_result(std::cin)); } |