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
#include<bits/stdc++.h>
using namespace std;
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  string input;
  cin >> input;
  uint64_t res = 0;
  for(int mask = 1;mask < 8;mask++){
    int cnt[3] = {0, 0, 0};
    map<uint64_t, int> m;
    m[0] = 1;
    for(auto i : input){
      cnt[i - 'a']++;
      int req = 300007;
      for(int bit = 0;bit < 3;bit++)
        if((1 << bit) & mask)
          req = min(req, cnt[bit]);
      uint64_t a = cnt[0], b = cnt[1], c = cnt[2];
      if((1 << 0) & mask) a -= req;
      if((1 << 1) & mask) b -= req;
      if((1 << 2) & mask) c -= req;
      uint64_t hasz = (a << 38) + (b << 19) + c;
      res += m[hasz];
      m[hasz]++;
    }
  }
  cout << res << '\n';
}