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
#include <iostream>
using namespace std;
typedef unsigned int uint;

bool cond(int x, int y, int z);

int main()
{
    string s;
    cin >> s;

    int letters[3] = { 0 };
    int counter = 0;
    for (uint i = 0; i < s.length(); ++i)
    {
        for (uint j = i; j < s.length(); ++j)
        {
            letters[0] = letters[1] = letters[2] = 0;
            for (uint k = i; k <= j; ++k)
            {
                switch (s[k])
                {
                case 'a':
                    letters[0]++;
                    break;
                case 'b':
                    letters[1]++;
                    break;
                default:
                    letters[2]++;
                    break;
                }
            }
            if (cond(letters[0], letters[1], letters[2]))
                ++counter;
        }
    }

    cout << counter;

    return 0;
}

bool cond(int x, int y, int z)
{
    if (x * y * z != 0)
        return x == y && y == z;
    if (x)
        return (y == x || y == 0) && (z == x || z == 0);
    if (y)
        return z == 0 || z == y;
    return z != 0;
}