#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define MAX_SIZE 17 char last[MAX_SIZE] = {0}; char current[MAX_SIZE] = {0}; long long result = 0; int real_length = -1; void increase_length(int &last_len, const int curr_len) { real_length = real_length + 1; if (last_len < MAX_SIZE) last_len++; memset(&last[curr_len], '0', last_len - curr_len); last[last_len] = '\0'; } void next_w(char *curr) { int last_len = strlen(last); int curr_len = strlen(curr); if (curr_len > last_len) { strcpy(last, curr); real_length = curr_len; } else { int cmp = strncmp(last, curr, curr_len); strncpy(last, curr, curr_len); if (cmp == 0) { if (real_length < MAX_SIZE) { bool max = true; for (int i = curr_len; i < last_len; ++i) if (last[i] != '9') { max = false; break; } if (max == true) increase_length(last_len, curr_len); else { long long value = std::atoll(&last[curr_len]); ++value; sprintf(&last[curr_len], "%.*lld", last_len - curr_len, value); } } } else { if (cmp > 0) // last > curr increase_length(last_len, curr_len); else { memset(&last[curr_len], '0', last_len - curr_len); last[last_len] = '\0'; } } } result += real_length - curr_len; } int main() { int N; scanf("%d", &N); while (N--) { scanf("%s", current); next_w(current); //printf("%s %d %d %lld\n", last, strlen(last), real_length, result); } printf("%lld\n", result); 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define MAX_SIZE 17 char last[MAX_SIZE] = {0}; char current[MAX_SIZE] = {0}; long long result = 0; int real_length = -1; void increase_length(int &last_len, const int curr_len) { real_length = real_length + 1; if (last_len < MAX_SIZE) last_len++; memset(&last[curr_len], '0', last_len - curr_len); last[last_len] = '\0'; } void next_w(char *curr) { int last_len = strlen(last); int curr_len = strlen(curr); if (curr_len > last_len) { strcpy(last, curr); real_length = curr_len; } else { int cmp = strncmp(last, curr, curr_len); strncpy(last, curr, curr_len); if (cmp == 0) { if (real_length < MAX_SIZE) { bool max = true; for (int i = curr_len; i < last_len; ++i) if (last[i] != '9') { max = false; break; } if (max == true) increase_length(last_len, curr_len); else { long long value = std::atoll(&last[curr_len]); ++value; sprintf(&last[curr_len], "%.*lld", last_len - curr_len, value); } } } else { if (cmp > 0) // last > curr increase_length(last_len, curr_len); else { memset(&last[curr_len], '0', last_len - curr_len); last[last_len] = '\0'; } } } result += real_length - curr_len; } int main() { int N; scanf("%d", &N); while (N--) { scanf("%s", current); next_w(current); //printf("%s %d %d %lld\n", last, strlen(last), real_length, result); } printf("%lld\n", result); return 0; } |