#include <bits/stdc++.h> using namespace std; const int MAX = 201000; char A[MAX + 1], B[MAX + 1]; int n; long long w; int main() { scanf("%d %s", &n, A); int a = strlen(A); for (int i = 1; i < n; ++i) { scanf("%s", B); int b = 0; int r = 0; while (B[b]) { if (r == 0) { if (B[b] < A[b]) { r = -1; } else if (B[b] > A[b]) { r = 1; } } A[b] = B[b]; ++b; } if (b > a) { a = b; } else if (r != 0) { w += a - b; for (int j = b, k = min(b + 10, a); j < k; ++j) { A[j] = '0'; } for (int j = a - 1, k = max(a - 10, b); j >= k; --j) { A[j] = '0'; } if (r < 0) { A[a] = '0'; ++a; ++w; } } else { w += a - b; int p = 1; for (int j = a - 1; j >= b && p; --j) { if (A[j] < '9') { ++A[j]; p = 0; } else { A[j] = '0'; } } if (p) { A[a] = '0'; ++a; ++w; } } } printf("%lld\n", w); 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 | #include <bits/stdc++.h> using namespace std; const int MAX = 201000; char A[MAX + 1], B[MAX + 1]; int n; long long w; int main() { scanf("%d %s", &n, A); int a = strlen(A); for (int i = 1; i < n; ++i) { scanf("%s", B); int b = 0; int r = 0; while (B[b]) { if (r == 0) { if (B[b] < A[b]) { r = -1; } else if (B[b] > A[b]) { r = 1; } } A[b] = B[b]; ++b; } if (b > a) { a = b; } else if (r != 0) { w += a - b; for (int j = b, k = min(b + 10, a); j < k; ++j) { A[j] = '0'; } for (int j = a - 1, k = max(a - 10, b); j >= k; --j) { A[j] = '0'; } if (r < 0) { A[a] = '0'; ++a; ++w; } } else { w += a - b; int p = 1; for (int j = a - 1; j >= b && p; --j) { if (A[j] < '9') { ++A[j]; p = 0; } else { A[j] = '0'; } } if (p) { A[a] = '0'; ++a; ++w; } } } printf("%lld\n", w); return 0; } |