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

typedef std::map <std::pair <int, int>, int> map;

long long operator~ (map &X)
{
  long long r = 0;
  for (auto [i, v]: X) r += v*(v-1ll)/2;
  X.clear ();
  return r;
}

int main ()
{
  map A, B, C, AB, BC, CA, ABC;
  int ab = 0, bc = 0, ca = 0;
  long long r = 0;
  while (1)
  {
    A[{}]++, B[{}]++, C[{}]++;
    AB[{ab, 0}]++, BC[{bc, 0}]++, CA[{ca, 0}]++;
    ABC[{ab, bc}]++;
    switch (getchar ())
    {
    case 'a':
      r += ~B + ~C + ~BC;
      ab++, ca--;
      break;
    case 'b':
      r += ~C + ~A + ~CA;
      bc++, ab--;
      break;
    case 'c':
      r += ~A + ~B + ~AB;
      ca++, bc--;
      break;
    default:
      r += ~A + ~B + ~C + ~AB + ~BC + ~CA + ~ABC;
      printf ("%lli\n", r);
      return 0;
    }
  }
}