#include <bits/stdc++.h> using namespace std; int T[3002][3]; string s; bool sprawdz (int p, int k) { int l = -1; bool u = true; for (int i=0; i<3; ++i) { const int r = T[k][i]-T[p-1][i]; if (r==0) continue; if (l==-1) l = r; if (l!=r) u = false; } return u; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin>>s; const int dl = s.size(); if (dl>3000) return 0; for (int i=1; i<=dl; ++i) { if (s[i-1]=='a') T[i][0] = T[i-1][0]+1; else T[i][0] = T[i-1][0]; if (s[i-1]=='b') T[i][1] = T[i-1][1]+1; else T[i][1] = T[i-1][1]; if (s[i-1]=='c') T[i][2] = T[i-1][2]+1; else T[i][2] = T[i-1][2]; } int cn=0; for (int i=1; i<=dl; ++i) { for (int j=i; j<=dl; ++j) { if (sprawdz(i, j)) cn++; } } cout<<cn; }
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 | #include <bits/stdc++.h> using namespace std; int T[3002][3]; string s; bool sprawdz (int p, int k) { int l = -1; bool u = true; for (int i=0; i<3; ++i) { const int r = T[k][i]-T[p-1][i]; if (r==0) continue; if (l==-1) l = r; if (l!=r) u = false; } return u; } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin>>s; const int dl = s.size(); if (dl>3000) return 0; for (int i=1; i<=dl; ++i) { if (s[i-1]=='a') T[i][0] = T[i-1][0]+1; else T[i][0] = T[i-1][0]; if (s[i-1]=='b') T[i][1] = T[i-1][1]+1; else T[i][1] = T[i-1][1]; if (s[i-1]=='c') T[i][2] = T[i-1][2]+1; else T[i][2] = T[i-1][2]; } int cn=0; for (int i=1; i<=dl; ++i) { for (int j=i; j<=dl; ++j) { if (sprawdz(i, j)) cn++; } } cout<<cn; } |