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
#include <iostream>
#include <string>

using namespace std;

#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define SIZE(x) ((int)(x).size())

int a, b, c;
int n;
int ans = 0;
string word;

int main()
{
    cin >> word;
    n = SIZE(word);

    REP(i, n)
    {
        a = b = c = 0;
        FORD(j, i, 0)
        {
            if (word[j] == 'a') a++;
            else if (word[j] == 'b') b++;
            else if (word[j] == 'c') c++;

            // if a == 0 -> only b and c are important
            if (a == 0)
            {
                if (b == 0)
                    ans++;
                else if (c == 0)
                    ans++;
                else if (b == c)
                    ans++;
            }
            // if b == 0 and a != 0
            else if (b == 0)
            {
                if (c == 0)
                    ans++;
                else if (a == c)
                    ans++;
            }
            // if c == 0 and a != 0 and b != 0
            else if (c == 0)
            {
                if (a == b)
                    ans++;
            }
            // no letter is equal to zero
            else if (a == b && b == c)
            {
                ans++;
            }
        }
    }

    cout << ans;
}
/*
aabbabcccba
*/