#include <iostream>
#include <string>
using namespace std;
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
unsigned long long suma=0;
string s;
cin>>s;
int l=s.size();
int z[3][l+1];
z[0][0]=0;
z[1][0]=0;
z[2][0]=0;
for(int i=1;i<=l;i++)
{
z[0][i]=z[0][i-1];
z[1][i]=z[1][i-1];
z[2][i]=z[2][i-1];
z[int(s[i-1])-97][i]++;
for(int j=0;j<i-2;j++)
{
int a=z[0][i]-z[0][j];
int b=z[1][i]-z[1][j];
int c=z[2][i]-z[2][j];
if(a==0)
{
if(b==0)
{
suma++;
}
else
{
if(b==c||c==0)
{
suma++;
}
}
}
else
{
if(b==0)
{
if(c==a||c==0)
{
suma++;
}
}
else
{
if(b==a&&(c==a||c==0))
{
suma++;
}
}
}
}
}
cout<<suma+2*l-1;
}
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 | #include <iostream> #include <string> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); unsigned long long suma=0; string s; cin>>s; int l=s.size(); int z[3][l+1]; z[0][0]=0; z[1][0]=0; z[2][0]=0; for(int i=1;i<=l;i++) { z[0][i]=z[0][i-1]; z[1][i]=z[1][i-1]; z[2][i]=z[2][i-1]; z[int(s[i-1])-97][i]++; for(int j=0;j<i-2;j++) { int a=z[0][i]-z[0][j]; int b=z[1][i]-z[1][j]; int c=z[2][i]-z[2][j]; if(a==0) { if(b==0) { suma++; } else { if(b==c||c==0) { suma++; } } } else { if(b==0) { if(c==a||c==0) { suma++; } } else { if(b==a&&(c==a||c==0)) { suma++; } } } } } cout<<suma+2*l-1; } |
English