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
/***********PA 2021***********/
/******Autor: Jakub Bąba******/

#include <bits/stdc++.h>
using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string napis; long long ilosc = 0;
  int i, j;
  int balans[300000][3];
  for (i=0; i<300000; i++)
  {
    for (j=0; j<3; j++)
      balans[i][j] = 0;
  }
  cin >> napis;


  for (i=0; i<napis.length(); i++)
  {
    for (j=0; j<i+1; j++)
    {
      if (napis[i] == 'a')
        balans[j][0]++;
      else if (napis[i] == 'b')
        balans[j][1]++;
      else
        balans[j][2]++;
      if (
        (!balans[j][1] && !balans[j][2]) || (!balans[j][0] && !balans[j][2]) ||  (!balans[j][0] && !balans[j][1]) ||
        (balans[j][0] == balans[j][1] && !balans[j][2]) || (balans[j][0] == balans[j][2] && !balans[j][1]) || (balans[j][1] == balans[j][2] && !balans[j][0]) ||
        (balans[j][0] == balans[j][1] && balans[j][0] == balans[j][2])
      )
        ilosc++;
    }
  }

  cout << ilosc;

  return 0;
}