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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <unordered_map>
#include <cmath>
#include <random>

using namespace std;

struct hash_tuple {
  
    template <class T1, class T2, class T3>
  
    size_t operator()(
        const tuple<T1, T2, T3>& x)
        const
    {
        return get<0>(x)
               ^ get<1>(x)
               ^ get<2>(x);
    }
};

string input_line;
unordered_map<tuple<int,int,int>,int,hash_tuple> sc, si;

void update_statistics(int from, int to){
    sc.clear();
    si.clear();
    int a = 0, b = 0, c = 0;
    for (int i = from; i < to; i++)
    {
        if(input_line[i] == 'a') a++;
        else if(input_line[i] == 'b') b++;
        else if(input_line[i] == 'c') c++;

        //cout << a << ' ' << b << ' ' << c << '\n';
        si[make_tuple(a,b,c)]++;
        //cout << si[make_tuple(0,1,0)] << ' ' << si[make_tuple(0,1,1)] << '\n';

        if(b == 0 && c == 0)
        {
            sc[make_tuple(0,-1,-1)]++;
        }

        else if(a == 0 && c == 0)
        {
            sc[make_tuple(-1,0,-1)]++;
        }

        else if(a == 0 && b == 0)
        {
            sc[make_tuple(-1,-1,0)]++;
        }

        else if(a == 0)
        {
            int mmin = min(b, c);
            sc[make_tuple(-1,b-mmin,c-mmin)]++;
        }

        else if(b == 0)
        {
            int mmin = min(a, c);
            sc[make_tuple(a-mmin,-1,c-mmin)]++;
        }

        else if(c == 0)
        {
            int mmin = min(a, b);
            sc[make_tuple(a-mmin,b-mmin,-1)]++;
        }
        else
        {
            int mmin = min(a, min(b,c));
            sc[make_tuple(a-mmin,b-mmin,c-mmin)]++;
        }
    }
}

int cout_left(int from, int to)
{
    //cout << si[make_tuple(0,1,0)] << ' ' << si[make_tuple(0,1,1)] << '\n';
    int a = 0, b = 0, c = 0, counter = 0;
    for (int i = to-1; i >= from; i--)
    {
        if(input_line[i] == 'a') a++;
        else if(input_line[i] == 'b') b++;
        else if(input_line[i] == 'c') c++;

        if(b == 0 && c == 0)
        {
            counter += sc[make_tuple(0,-1,-1)];//a
            counter += si[make_tuple(0,a,0)];//b
            counter += si[make_tuple(0,0,a)];//c

            counter += sc[make_tuple(0,a,-1)];//ab
            counter += sc[make_tuple(0,-1,a)];//ac
            counter += si[make_tuple(0,a,a)];//bc

            counter += sc[make_tuple(0,a,a)];//abc
        }

        else if(a == 0 && c == 0)
        {
            counter += si[make_tuple(b,0,0)];//a
            counter += sc[make_tuple(-1,0,-1)];//b
            counter += si[make_tuple(0,0,b)];//c

            counter += sc[make_tuple(b,0,-1)];//ab
            counter += si[make_tuple(b,0,b)];//ac
            counter += sc[make_tuple(-1,0,b)];//bc

            counter += sc[make_tuple(b,0,b)];//abc
        }

        else if(a == 0 && b == 0)
        {
            counter += si[make_tuple(c,0,0)];//a
            counter += si[make_tuple(0,c,0)];//b
            counter += sc[make_tuple(-1,-1,0)];//c

            counter += si[make_tuple(c,c,0)];//ab
            counter += sc[make_tuple(c,-1,0)];//ac
            counter += sc[make_tuple(-1,c,0)];//bc

            counter += sc[make_tuple(c,c,0)];//abc
        }

        else if(a == 0)
        {
            int mmax = max(b, c);

            if(b==c) counter += si[make_tuple(b,0,0)];//a
            counter += si[make_tuple(0,c-b,0)];//b
            counter += si[make_tuple(0,0,b-c)];//c

            if(b!=c) counter += si[make_tuple(c,c-b,0)];//ab
            if(b!=c) counter += si[make_tuple(b,0,b-c)];//ac
            counter += sc[make_tuple(-1,mmax-b,mmax-c)];//bc

            counter += sc[make_tuple(mmax,mmax-b,mmax-c)];//abc
        }

        else if(b == 0)
        {
            int mmax = max(a, c);

            counter += si[make_tuple(c-a,0,0)];//a
            if(a==c) counter += si[make_tuple(0,a,0)];//b
            counter += si[make_tuple(0,0,a-c)];//c

            if(a!=c) counter += si[make_tuple(c-a,c,0)];//ab, b
            counter += sc[make_tuple(mmax-a,-1,mmax-c)];//ac
            if(a!=c) counter += si[make_tuple(0,a,a-c)];//bc

            counter += sc[make_tuple(mmax-a,mmax,mmax-c)];//abc
        }

        else if(c == 0)
        {
            int mmax = max(a, b);

            counter += si[make_tuple(b-a,0,0)];//a
            counter += si[make_tuple(0,a-b,0)];//b
            if(b==a) counter += si[make_tuple(0,0,b)];//c

            counter += sc[make_tuple(mmax-a,mmax-b,-1)];//ab
            if(a!=b) counter += si[make_tuple(b-a,0,b)];//ac
            if(a!=b)counter += si[make_tuple(0,a-b,a)];//bc

            counter += sc[make_tuple(mmax-a,mmax-b,mmax)];//abc
        }
        else
        {
            int mmax = max(a,max(b,c));

            if(c==b) counter += si[make_tuple(b-a,0,0)];//a
            if(a==c)counter += si[make_tuple(0,a-b,0)];//b
            if(b==a) counter += si[make_tuple(0,0,a-c)];//c

            if(mmax-a != 0 && mmax-b != 0) counter += si[make_tuple(mmax-a,mmax-b,0)];//ab
            if(mmax-a != 0 && mmax-c != 0) counter += si[make_tuple(mmax-a,0,mmax-c)];//ac
            if(mmax-b != 0 && mmax-c != 0) counter += si[make_tuple(0,mmax-b,mmax-c)];//bc

            counter += sc[make_tuple(mmax-a,mmax-b,mmax-c)];//abc
        }
    }
    //cout << counter << '\n';

    return counter;
}

int run(int from, int to)
{
    int size = to-from;
    if(size == 1) return 1;
    if(size == 2) return 3;
    int middle_point = size/2 + from;
    update_statistics(middle_point, to);
    return cout_left(from, middle_point) + run(from, middle_point) + run(middle_point, to);
}

void random_test_gen()
{
    int size = (rand() % 100) + 1;
    vector<char> v;
    for (int i = 0; i < size; i++)
    {
        v.push_back((char)((rand()%3)+'a'));
    }
    input_line = string(v.data(), size);
}

int bf(){
    vector<int> c[3];
    for (int i = 0; i < 3; i++)
    {
        c[i].push_back(0);
        int status = 0;
        for (int j = 0; j < input_line.length(); j++)
        {
            if(input_line[j] == i+'a') status++;
            c[i].push_back(status);
        }
        
    }

    int counter = 0;

    for (int i = 0; i < input_line.length(); i++)
    {
        for (int j = i; j < input_line.length(); j++)
        {
            int t[3];
            for (int k = 0; k < 3; k++)
            {
                t[k] = c[k][j+1] - c[k][i];
            }

            int mmax = max(t[0],max(t[1],t[2]));

            if((t[0] == 0 || t[0] == mmax) && (t[1] == 0 || t[1] == mmax) && (t[2] == 0 || t[2] == mmax))
            counter++;
        }
        
    }
    
    return counter;
     
}

int main()
{
    // srand(123);
    // while (true)
    // {
    //     random_test_gen();
    //     int bfv = bf();
    //     int fast = run(0, input_line.size());
    //     cout << input_line << '\n';
    //     cout << bfv << '\n';
    //     cout << fast << '\n';
    //     if(bfv != fast) return 0;
    // }
    
    getline(cin, input_line);
    //random_test_gen();
    //cout << input_line << '\n';
    cout << run(0, input_line.size()) << '\n';
    //cout << bf() << '\n';
}