#include <bits/stdc++.h> using namespace std; struct Pod{ int a; int b; int c; }; Pod pref[300003]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int a=0, b=0, c=0; long long out=0; for(int i=0; i<s.size(); i++){ if(s[i]=='a'){ pref[i+1].a=pref[i].a+1; pref[i+1].b=pref[i].b; pref[i+1].c=pref[i].c; }else if(s[i]=='b'){ pref[i+1].a=pref[i].a; pref[i+1].b=pref[i].b+1; pref[i+1].c=pref[i].c; }else{ pref[i+1].a=pref[i].a; pref[i+1].b=pref[i].b; pref[i+1].c=pref[i].c+1; } } for(int i=1; i<=s.size(); i++){ for(int j=i; j<=s.size(); j++){ a=pref[j].a-pref[i-1].a; b=pref[j].b-pref[i-1].b; c=pref[j].c-pref[i-1].c; if(a==0)a=max(b,c); if(b==0)b=max(a,b); if(c==0)c=max(a,c); if(a==b && b==c){ out++; } } } cout << out; 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 | #include <bits/stdc++.h> using namespace std; struct Pod{ int a; int b; int c; }; Pod pref[300003]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int a=0, b=0, c=0; long long out=0; for(int i=0; i<s.size(); i++){ if(s[i]=='a'){ pref[i+1].a=pref[i].a+1; pref[i+1].b=pref[i].b; pref[i+1].c=pref[i].c; }else if(s[i]=='b'){ pref[i+1].a=pref[i].a; pref[i+1].b=pref[i].b+1; pref[i+1].c=pref[i].c; }else{ pref[i+1].a=pref[i].a; pref[i+1].b=pref[i].b; pref[i+1].c=pref[i].c+1; } } for(int i=1; i<=s.size(); i++){ for(int j=i; j<=s.size(); j++){ a=pref[j].a-pref[i-1].a; b=pref[j].b-pref[i-1].b; c=pref[j].c-pref[i-1].c; if(a==0)a=max(b,c); if(b==0)b=max(a,b); if(c==0)c=max(a,c); if(a==b && b==c){ out++; } } } cout << out; return 0; } |