#include <iostream> #include <map> using namespace std; map<long long,long long>mapka_ab; map<long long,long long>mapka_ac; map<long long,long long>mapka_bc; map<pair<long long,long long>,long long>mapka_ab_bc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; int ab=0,ac=0,bc=0; long long l=0,licznik=0; for(int i=0;i<(int)s.length();i++) { mapka_ab[ab]++; mapka_ac[ac]++; mapka_bc[bc]++; mapka_ab_bc[{ab,bc}]++; if(s[i]=='a') { mapka_bc.clear(); ab++; ac++; l+=mapka_ab[ab]; l+=mapka_ac[ac]; } else if(s[i]=='b') { mapka_ac.clear(); ab--; bc++; l+=mapka_ab[ab]; l+=mapka_bc[bc]; } else { mapka_ab.clear(); ac--; bc--; l+=mapka_ac[ac]; l+=mapka_bc[bc]; } l+=mapka_ab_bc[{ab,bc}]; if(i==0) { licznik++; l+=licznik; } else { if(s[i-1]==s[i]) { licznik++; l+=licznik; } else { licznik=1; l+=licznik; } } } cout<<l; 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 67 68 69 70 71 72 | #include <iostream> #include <map> using namespace std; map<long long,long long>mapka_ab; map<long long,long long>mapka_ac; map<long long,long long>mapka_bc; map<pair<long long,long long>,long long>mapka_ab_bc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; int ab=0,ac=0,bc=0; long long l=0,licznik=0; for(int i=0;i<(int)s.length();i++) { mapka_ab[ab]++; mapka_ac[ac]++; mapka_bc[bc]++; mapka_ab_bc[{ab,bc}]++; if(s[i]=='a') { mapka_bc.clear(); ab++; ac++; l+=mapka_ab[ab]; l+=mapka_ac[ac]; } else if(s[i]=='b') { mapka_ac.clear(); ab--; bc++; l+=mapka_ab[ab]; l+=mapka_bc[bc]; } else { mapka_ab.clear(); ac--; bc--; l+=mapka_ac[ac]; l+=mapka_bc[bc]; } l+=mapka_ab_bc[{ab,bc}]; if(i==0) { licznik++; l+=licznik; } else { if(s[i-1]==s[i]) { licznik++; l+=licznik; } else { licznik=1; l+=licznik; } } } cout<<l; return 0; } |