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
#include<cstdio>
#include<algorithm>

using namespace std;

static char str[300002];

struct wek{
    int w1;
    int w2;

    bool operator<(wek a){
        if(w1 == a.w1){
            return w2 < a.w2;
        }
        return w1 < a.w1;
    }

    bool operator==(wek a){
        return (w1==a.w1) && (w2==a.w2);
    }
};

static wek tab[300001];

typedef unsigned long long ull;

int main(){
    scanf(" %s", str + 1);
    ull wynik = 0;

    const int od[7][6] = {
        {-1,-1,1,0,0,1},//abc
        {-1,0,1,0,0,1},//ab
        {-1,0,0,1,1,0},//ac
        {0,1,-1,0,1,0},//bc
        {0,0,0,1,0,1},//a
        {0,1,0,0,0,1},//b
        {0,1,0,1,0,0}//c
    };

    for(int k = 0; k < 7; ++k){
        int s = 1;
        for(; str[s]; ++s){
            tab[s] = tab[s-1];
            switch(str[s]){
                case 'a':
                    tab[s].w1+=od[k][0];
                    tab[s].w2+=od[k][1];
                    break;
                case 'b':
                    tab[s].w1+=od[k][2];
                    tab[s].w2+=od[k][3];
                    break;
                case 'c':
                    tab[s].w1+=od[k][4];
                    tab[s].w2+=od[k][5];
                    break;
            }
        }

        sort(tab, tab+s);

        ull ile = 1;

        for(int i = 1; i < s; ++i){
            if(tab[i] == tab[i-1]){
                ++ile;
            }
            else{
                wynik += ile * (ile - 1) / 2;
                ile = 1;
            }
        }
        wynik += ile * (ile - 1) / 2;
    }

    printf("%llu\n", wynik);

    return 0;
}