#include <iostream>
#define REP(x,n) for(int x=0;x<(n);++x)
//#define DEBUG_LEVEL -1
#ifdef DEBUG_LEVEL
#define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}}
#else
#define DEBUG(level, x)
#endif
using namespace std;
const int MAX_N = 1000001;
enum State {
SUM_EQUAL,
SUM_MINUS_ONE,
SUM_EQUAL_WITH_OFFSET,
SUM_INVALID
};
string a,b,c;
int main() {
cin >> a >> b >> c;
long long regionGroups = 0;
State state = SUM_INVALID;
int n = a.size();
long long result = 0;
#define END_REGION {\
result += regionGroups * (regionGroups + 1) / 2; \
DEBUG(0, cerr << x << ") adding region of size " << regionGroups << endl;) \
regionGroups = 0; \
}
REP(x,n) {
State newState;
int diff = a[x] + b[x] - c[x] - '0'; // equals to (a[x] - '0') + (b[x] - '0') - (c[x] - '0');
DEBUG(-1, cerr << " calculateState for " << a[x] << " " << b[x] << " " << c[x] << " with previous state " << state << " gives diff " << diff << endl;)
if (diff == 0) {
if (state == SUM_MINUS_ONE) {
END_REGION;
}
newState = SUM_EQUAL;
} else if (diff == -1) {
if (state == SUM_MINUS_ONE) {
END_REGION;
}
newState = SUM_MINUS_ONE;
} else if (diff == 10 && state == SUM_MINUS_ONE) {
newState = SUM_EQUAL;
} else if (diff == 9 && state == SUM_MINUS_ONE) {
newState = SUM_MINUS_ONE;
} else {
newState = SUM_INVALID;
END_REGION;
}
DEBUG(-1, cerr << " State for position " << x << " is " << newState << endl;)
if (newState == SUM_EQUAL) {
++regionGroups;
}
state = newState;
}
#ifdef DEBUG_LEVEL
int x = n;
#endif
END_REGION;
cout << result << endl;
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 82 83 84 | #include <iostream> #define REP(x,n) for(int x=0;x<(n);++x) //#define DEBUG_LEVEL -1 #ifdef DEBUG_LEVEL #define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}} #else #define DEBUG(level, x) #endif using namespace std; const int MAX_N = 1000001; enum State { SUM_EQUAL, SUM_MINUS_ONE, SUM_EQUAL_WITH_OFFSET, SUM_INVALID }; string a,b,c; int main() { cin >> a >> b >> c; long long regionGroups = 0; State state = SUM_INVALID; int n = a.size(); long long result = 0; #define END_REGION {\ result += regionGroups * (regionGroups + 1) / 2; \ DEBUG(0, cerr << x << ") adding region of size " << regionGroups << endl;) \ regionGroups = 0; \ } REP(x,n) { State newState; int diff = a[x] + b[x] - c[x] - '0'; // equals to (a[x] - '0') + (b[x] - '0') - (c[x] - '0'); DEBUG(-1, cerr << " calculateState for " << a[x] << " " << b[x] << " " << c[x] << " with previous state " << state << " gives diff " << diff << endl;) if (diff == 0) { if (state == SUM_MINUS_ONE) { END_REGION; } newState = SUM_EQUAL; } else if (diff == -1) { if (state == SUM_MINUS_ONE) { END_REGION; } newState = SUM_MINUS_ONE; } else if (diff == 10 && state == SUM_MINUS_ONE) { newState = SUM_EQUAL; } else if (diff == 9 && state == SUM_MINUS_ONE) { newState = SUM_MINUS_ONE; } else { newState = SUM_INVALID; END_REGION; } DEBUG(-1, cerr << " State for position " << x << " is " << newState << endl;) if (newState == SUM_EQUAL) { ++regionGroups; } state = newState; } #ifdef DEBUG_LEVEL int x = n; #endif END_REGION; cout << result << endl; return 0; } |
English