#include <iostream> #include <string> using namespace std; int a[300005] {}; int b[300005] {}; int c[300005] {}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; bool xa=0,xb=0,xc=0; int x=0,maxi=0,la=0,lb=0,lc=0; long long int ile=0; cin>>s; for (int i=1;i<=s.size();i++) { a[i]=a[i-1]+a[i]; b[i]=b[i-1]+b[i]; c[i]=c[i-1]+c[i]; if (s[i-1]=='a') {a[i]++;} else if (s[i-1]=='b') {b[i]++;} else if (s[i-1]=='c') {c[i]++;} } if ((a[s.size()]==s.size()) || (b[s.size()]==s.size()) || (c[s.size()]==s.size())) {cout<<((s.size())*(s.size()+1))/2; return 0;} ile=ile+(s.size()*2)-1; for (int i=0;i<=s.size()-3;i++) { x=3; while (i+x<=s.size()) { la=a[i+x]-a[i]; lb=b[i+x]-b[i]; lc=c[i+x]-c[i]; xa=la; xb=lb; xc=lc; maxi=max(la,lb); maxi=max(maxi,lc); if ( (la==maxi*xa) && (lb==maxi*xb) && (lc==maxi*xc) ) {ile++; x=x+xa+xb+xc;} else {x=maxi*(xa+xb+xc);} } } cout<<ile; return 0; }
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 64 65 66 | #include <iostream> #include <string> using namespace std; int a[300005] {}; int b[300005] {}; int c[300005] {}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; bool xa=0,xb=0,xc=0; int x=0,maxi=0,la=0,lb=0,lc=0; long long int ile=0; cin>>s; for (int i=1;i<=s.size();i++) { a[i]=a[i-1]+a[i]; b[i]=b[i-1]+b[i]; c[i]=c[i-1]+c[i]; if (s[i-1]=='a') {a[i]++;} else if (s[i-1]=='b') {b[i]++;} else if (s[i-1]=='c') {c[i]++;} } if ((a[s.size()]==s.size()) || (b[s.size()]==s.size()) || (c[s.size()]==s.size())) {cout<<((s.size())*(s.size()+1))/2; return 0;} ile=ile+(s.size()*2)-1; for (int i=0;i<=s.size()-3;i++) { x=3; while (i+x<=s.size()) { la=a[i+x]-a[i]; lb=b[i+x]-b[i]; lc=c[i+x]-c[i]; xa=la; xb=lb; xc=lc; maxi=max(la,lb); maxi=max(maxi,lc); if ( (la==maxi*xa) && (lb==maxi*xb) && (lc==maxi*xc) ) {ile++; x=x+xa+xb+xc;} else {x=maxi*(xa+xb+xc);} } } cout<<ile; return 0; } |