#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
string a, b, c;
cin >> a >> b >> c;
int n = a.length();
long long answer = 0;
pair<long long, long long> dp = {0, 0};
for (int i = 0; i < n; i++) {
int x = a[i] - '0';
int y = b[i] - '0';
int z = c[i] - '0';
dp.first++; // zero
if (x + y == z) {
dp = {dp.first, 0};
}
else if (x + y == z + 10) {
dp = {dp.second, 0};
}
else if (x + y + 1 == z) {
dp = {0, dp.first};
}
else if (x + y + 1 == z + 10) {
dp = {0, dp.second};
}
else {
dp = {0, 0};
}
answer += dp.first;
}
cout << answer << 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string a, b, c; cin >> a >> b >> c; int n = a.length(); long long answer = 0; pair<long long, long long> dp = {0, 0}; for (int i = 0; i < n; i++) { int x = a[i] - '0'; int y = b[i] - '0'; int z = c[i] - '0'; dp.first++; // zero if (x + y == z) { dp = {dp.first, 0}; } else if (x + y == z + 10) { dp = {dp.second, 0}; } else if (x + y + 1 == z) { dp = {0, dp.first}; } else if (x + y + 1 == z + 10) { dp = {0, dp.second}; } else { dp = {0, 0}; } answer += dp.first; } cout << answer << endl; } |
English