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
//potyczki algorytmiczne 2021
//runda probna
//Zadanie: Zbalansowane slowa
//autor: AT

#include <bits/stdc++.h>
using namespace std;
    int tab[300007][3]={0};
    void wypiszTab(int n)
    {
        for(int i = 0; i<=n; i++)
        {
            cout << tab[i][0] << ' ';
        }
        cout << endl;
        for(int i = 0; i <= n; i++)
        {
            cout << tab[i][1] << ' ';
        }
        cout << endl;
        for(int i = 0; i<=n ; i++)
        {
            cout << tab[i][2] << ' ';
        }
        cout << endl;
    }
int main()
{
    string slowo;
    cin >> slowo;

    for(unsigned int i=0;i<slowo.length();i++)
    {
        if(slowo[i] == 'a')
        {
            tab[i+1][0] = tab[i][0] + 1;
            tab[i+1][1] = tab[i][1];
            tab[i+1][2] = tab[i][2];
        }
        else if(slowo[i] == 'b')
        {
            tab[i+1][0] = tab[i][0] ;
            tab[i+1][1] = tab[i][1] + 1;
            tab[i+1][2] = tab[i][2];
        }
        else if(slowo[i] == 'c')
        {
           tab[i+1][0] = tab[i][0] ;
            tab[i+1][1] = tab[i][1] ;
            tab[i+1][2] = tab[i][2] + 1 ;
        }
    }
    //wypiszTab(slowo.length());
    int a, b, c;
    unsigned long long int wynik = 0;
    for(unsigned int i = 1; i <= slowo.length(); i++)
    {
        for(unsigned int j = i; j <= slowo.length(); j++)
        {
            a = tab[j][0] - tab[i - 1][0];
            b = tab[j][1] - tab[i - 1][1];
            c = tab[j][2] - tab[i - 1][2];
            if((a == 0 and b == 0) or (a == 0 and c == 0) or (b == 0 and c == 0)) wynik++;
            else if((a == b and c == 0) or (b == c and a == 0) or ( a == c and b == 0)) wynik++;
            else if(a == b and b == c and a == c) wynik++;
            /*
            for(int k=i; k <=j; k++)
                cout << slowo[k];
            cout << "   i=" << i << " j=" << j << " a=" << a << " b=" << b << " c=" << c << " wynik=" << wynik << endl;
            */
        }
    }
    cout << wynik << endl;

}

//aabbabcccba