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
#include <iostream>
#include <string>
using namespace std;
int const max_N = 1000005;
int numA[max_N];
int numB[max_N];
int numC[max_N];
int type[max_N];
long long total_count = 0;
bool unfinished_string = false;
long long streak = 0;

void break_streak() {
    unfinished_string = false;
    total_count += streak * (streak + 1) / 2;
    streak = 0;
}

void process_anew(int cur_type) {
    if ((cur_type & 2) == 0) {
        if ((cur_type & 1) == 0) streak++;
        else {
            unfinished_string = true;
        }
    }
    else {
        break_streak();
    }
}

int main()
{
    int n = 0;
    string num;
    cin >> num;
    n = num.size();
    for (int i = 0; i < n; i++) {
        numA[i] = num[i] - '0';
    }
    cin >> num;
    for (int i = 0; i < n; i++) {
        numB[i] = num[i] - '0';
    }
    cin >> num;
    for (int i = 0; i < n; i++) {
        numC[i] = num[i] - '0';
    }
    for (int i = 0; i < n; i++) {
        if (numA[i] + numB[i] == numC[i]) {
            type[i] = 0;
            continue;
        }
        if (numA[i] + numB[i] == 10 + numC[i]) {
            type[i] = 2;
            continue;
        }
        if (numA[i] + numB[i] + 1 == numC[i]) {
            type[i] = 1;
            continue;
        }
        if (numA[i] + numB[i] + 1 == 10 + numC[i]) {
            type[i] = 3;
            continue;
        }
        type[i] = -1;
    }
    for (int i = 0; i < n; i++) {
        if (type[i] == -1) {
            break_streak();
            continue;
        }
        if (unfinished_string) {
            if ((type[i - 1] & 1) != ((type[i] & 2) >> 1)) {
                break_streak();
                process_anew(type[i]);
                continue;
            }
            if ((type[i] & 1) == 0) {
                unfinished_string = false;
                streak++;
            }
        }
        else {
            process_anew(type[i]);
        }
    }
    break_streak();
    cout << total_count;
}