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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <bits/stdc++.h>

#include <unordered_map>

using namespace std;
#define LL long long

#define DBG(X) 

int match(int a, int b, int c)
{
    if (a < b) swap(a,b);
    if (a < c) swap(a,c);
    if (b < c) swap(b,c);
    if (a < b) swap(a,b);
    if (b == 0 && c == 0) return 1;
    if (c == 0) return a == b ? 1 : 0;
    return ((a==b) && (b==c)) ? 1 : 0;
}

vector<vector<int> >  precompute(const string& s)
{
    vector<vector<int> > cnt(3, vector<int>(s.size()));
    for (int i=0; i < s.size(); i++)
    {
        for (int j=0; j < 3; j++)
        {
            if (i) cnt[j][i] = cnt[j][i-1];
            if (j+'a'==s[i]) cnt[j][i]++;
            DBG(printf("cnt[%c][%d]=%d\n", j+'a',i, cnt[j][i]));
        }
    }
    return cnt;
}

long long solveSlow(const string& s, const vector<vector<int> > &cnt, int b, int e) 
{
    LL res = 0;
    for (int i=b; i <= e; i++)
      for (int j=i; j <= e; j++)
      {
          int ile[3] = {0};
          for (int k=0; k < 3; k++)
          {
              ile[k] = cnt[k][j];
              if (i-1>=0) ile[k] -= cnt[k][i-1];
          }
          //printf("(%d,%d) -> ile['a']=%d, ile['b']=%d, ile['c']=%d, res=%lld\n", i, j, ile[0], ile[1], ile[2], res);
          res += match(ile[0], ile[1], ile[2]);
         
      }
      DBG(printf("solveSlow(%d,%d)=%lld\n", b,e,res));
      return res;
}

const unsigned char ABC_MASK[3] = {1,2,4};

struct ABC {
    int cnt[3];
    unsigned char mask;

    ABC() 
    {
        cnt[0] = 0;
        cnt[1] = 0;
        cnt[2] = 0;
        mask = 0;
    }
    ABC(int ac, int bc, int cc)
    {
        cnt[0] = ac;
        cnt[1] = bc;
        cnt[2] = cc;
        mask = 0;
        for (int i=0; i < 3; i++)
        {
          if (cnt[i]) 
          {
              mask |= ABC_MASK[i];
          }
        }
    }

    ABC(int ac, int bc, int cc, unsigned char _mask) : ABC(ac,bc,cc) {
        this->mask = _mask;
    }

    void add(char c)
    {
        int x = c - 'a';
        ++cnt[x];
        if (cnt[x]) mask |= ABC_MASK[x];
    }
    
    struct HashFunction
    {
        size_t operator()(const ABC& abc) const
        {
            size_t hash = 0;
            for (int i=0; i < 3; i++)
            {
                if (abc.mask & ABC_MASK[i])
                {
                    hash += abc.cnt[i];
                    hash *= 100333;
                }
            }
            return hash;
        }
    };

    bool operator==(const ABC &other) const {
       if (mask != other.mask) return false;
       for (int i=0; i < 3; i++)
       {
           if ((mask&ABC_MASK[i]) && (cnt[i] != other.cnt[i])) return false;
       }
       return true;
    }

    string asString() const
    {
        string res;
        //if (mask & ABC_MASK[0]) 
        res += "a(" + to_string(cnt[0]) + ")";
       
        //if (mask & ABC_MASK[1]) 
        res += "b(" + to_string(cnt[1]) + ")";
        
        //if (mask & ABC_MASK[2]) 
        res += "c(" + to_string(cnt[2]) + ")";
        
        res += "mask="+to_string(mask);
        return res;
    }

    ABC compress() const
    {
        int cmprCnt[3] = {0,0,0};
        int baseline;
        baseline = 1000000000;
        for (int i=0; i < 3; i++)
        {
            if (mask & ABC_MASK[i])
            {
                baseline = min(baseline, cnt[i]);
            }
        }
        for (int i=0; i < 3; i++)
        {
            if (mask & ABC_MASK[i])
            {
                cmprCnt[i] = cnt[i] - baseline;
            }
        }
        return ABC(cmprCnt[0], cmprCnt[1], cmprCnt[2], mask);
    }

    int max_value_by_mask(unsigned char aMask) const {
        int res = -1;
        for (int i=0; i < 3; i++)
        {
            if (aMask & ABC_MASK[i])
            {
                res = max(cnt[i], res);
            }
        }
        return res;
    }

    bool hasNonZeroAt(unsigned char aMask)
    {
        for (int i=0; i < 3; i++)
        {
            if (!(aMask&ABC_MASK[i]))
            {
                if (cnt[i]>0) return true;
            }
        }
        return false;
    }
};


ABC invertWithMask(const ABC& abc, unsigned char mask)
{
    int mx = abc.max_value_by_mask(abc.mask);
    int cnt[3] = {0,0,0};
    for (int i=0; i < 3; i++)
    {
        if ((mask | abc.mask) & ABC_MASK[i])
        {
            cnt[i] = mx - abc.cnt[i];
        }
    }
    return ABC(cnt[0],cnt[1],cnt[2], mask);
}

#define MAP_TYPE unordered_map<ABC, int, ABC::HashFunction>

struct ABCFinder {
    MAP_TYPE MCombCompressed[8];
    MAP_TYPE MCombOriginal[8];


    void addCompressed(const ABC &compressed)
    {
        MAP_TYPE &M = MCombCompressed[compressed.mask];
        DBG(printf("AddCompressed(%s) before=%d\n", compressed.asString().c_str(), M.size()));
        if (M.find(compressed) == M.end())
        {
            M[compressed] = 1;
            return;
        }
        ++M[compressed];
    }

    void addOriginal(const ABC &abc)
    {
        DBG(printf("AddOriginal(%s)\n", abc.asString().c_str()));
        MAP_TYPE &M = MCombOriginal[abc.mask];
        if (M.find(abc) == M.end())
        {
            M[abc] = 1;
            return;
        }
        ++M[abc];
    }

    void add(const ABC &abc)
    {
        addOriginal(abc);
        addCompressed(abc.compress());
    }

    int countCompressed(const ABC &compressed)
    {
        MAP_TYPE &M = MCombCompressed[compressed.mask];
        DBG(printf("countCompressed for %s\n", compressed.asString().c_str()));
        auto it = M.find(compressed);
        if (it == M.end())
        {
            return 0;
        }
        
        int res = M[compressed];
        DBG(printf("OOOO res=%d\n", res));
        return res;
    }

    int countOriginal(const ABC &abc)
    {
        MAP_TYPE &M = MCombOriginal[abc.mask];
        auto it = M.find(abc);
        if (it == M.end())
        {
            return 0;
        }
        DBG(printf("countOriginal for %s\n", abc.asString().c_str()));
        int res = M[abc];
        DBG(printf("OOOO res=%d\n", res));
        return res;
    }

    LL countMatching(const ABC &original)
    {
        LL res = 0;
        DBG(printf("countMatchings for %s\n", original.asString().c_str()));
        for (unsigned char destMask=1; destMask <= 7; destMask++)
        {
            if ((original.mask & destMask) != original.mask) continue;
            for (unsigned char additionalMask=1; additionalMask <= 7; additionalMask++)
            {
                if ((additionalMask & destMask) != additionalMask) continue;
                if ((additionalMask | original.mask) != destMask) continue;
                if (original.mask == additionalMask)
                {
                    DBG(printf("case 1\n"));
                    res += countCompressed(invertWithMask(original, additionalMask).compress());
                }
                else if ((original.mask & additionalMask) == original.mask)
                {
                    // to znaczy ze additionalMask ma wiecej liter niz orginal
                    // wiec odpowiedz to bedzie
                    ABC inverted = invertWithMask(original, additionalMask);
                    DBG(printf("case 2, destMask=%d original=%s inverted=%s compressed=%s\n", destMask, original.asString().c_str(), inverted.asString().c_str(), inverted.compress().asString().c_str()));
                    
                    res += countCompressed(inverted.compress());
                }
                else
                {
                    // tzn. ze maska orginalna ma jakies litery dodatkowe, czyli musimy poszukac dokladnie takiego paternu jak ma additionalMask
                    ABC inverted = invertWithMask(original, original.mask | additionalMask);
                    DBG(printf("case 3, destMask=%d original=%s inverted=%s hasNonZero=%d\n", destMask, original.asString().c_str(), inverted.asString().c_str(), inverted.hasNonZeroAt(additionalMask)));

                    if (!inverted.hasNonZeroAt(additionalMask))
                    {
                        inverted.mask = additionalMask;
                        res += countOriginal(inverted);
                    }
                }
            }
        }
        
        return res;
    }
};

int getIndexOf(const string& s, int b, int e)
{
    if (1+e-b > 12) return -1;
    int res = 1;
    for (int i=b; i <= e; i++)
    {
        res *= 3;
        res += (s[i] - 'a');
    } 
    //printf("getIndexOf=%d\n", res);
    return res;
}

long long solve(const string& s, const vector<vector<int> > &cnt, vector<short> &smallAnsHash, int b, int e)
{
    if (e==b) return 1;
    if (e==b+1) return 3;
    int Hidx = getIndexOf(s, b, e);
   
    if (Hidx >= 0 && Hidx < smallAnsHash.size())
    {
        int a = smallAnsHash[Hidx];
        if (a)
        {
            return a;
        }
    }
 
    int med = (b+e) / 2;
    LL res_firsthalf = solve(s, cnt, smallAnsHash, b, med);
    LL res_secondhalf = solve(s, cnt, smallAnsHash, med+1, e);
    LL res = res_firsthalf + res_secondhalf;
    DBG(printf("Chosen MED=%d startRes=%lld\n", med, res));
    
    ABCFinder abcFinder;
    ABC current = ABC();
    DBG(printf("iterating upper half from med+1=%d to e=%d\n", med+1, e));
    for (int i=med+1; i <= e; i++)
    {
        current.add(s[i]);
        //DBG(printf("added letter %c, current=%s\n", s[i], current.asString().c_str()));
        abcFinder.add(current);
    }
    
    current = ABC();
    DBG(printf("Iterating lower half, from i=%d to b=%d\n", med, b));
    for (int i=med; i >=b; i--)
    {
        current.add(s[i]);
        res += abcFinder.countMatching(current);
    }
    if (Hidx >= 0 && Hidx < smallAnsHash.size())
    {
        smallAnsHash[Hidx] = res;
    }
    return res;
}

#define ASSERT_EQUAL(I, MASK, E) printf("input=%s, expected=%s, actual=%s, result=%s\n", I.asString().c_str(), E.asString().c_str(), invertWithMask(I, MASK).asString().c_str(), (!((E) == ( invertWithMask(I, MASK)))) ? "DIFFERENT" : "OK" ); 

char buf[300010];

int main() {
    
    scanf("%s", buf);
    string s(buf);
    
    vector<vector<int> > cnt = precompute(s);
    vector<short> smallAnsHash(600000, 0);
    printf("%lld\n", solve(s, cnt, smallAnsHash, 0, s.size()-1));
    return 0;
}