#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll M=1e6; ll solve(string &s, ll k) { ll ile[k], ans=0; rep(i, k) ile[i]=0; map<ll,ll>mp; mp[0]=1; rep(i, s.size()) { ++ile[s[i]-'a']; ll p=0, mi=M; rep(j, k) mi=min(mi, ile[j]); rep(j, k) p=p*M+ile[j]-mi; ans+=mp[p]; ++mp[p]; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; ll ans=0; for(int i=1; i<8; ++i) { string akt=""; ll k=0; rep(j, 3) if(i&(1<<j)) ++k; rep(j, s.size()) { bool ok=false; rep(l, 3) if((i&(1<<l)) && s[j]-'a'==l) ok=true; if(ok) { if(s[j]=='c') { if((i&1) && (i&2)) akt+="c"; else if((i&1) || (i&2)) akt+="b"; else akt+="a"; } else if(s[j]=='b') { if((i&1)) akt+="b"; else akt+="a"; } else akt+="a"; } else { ans+=solve(akt, k); akt=""; } } ans+=solve(akt, k); } cout << ans << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll M=1e6; ll solve(string &s, ll k) { ll ile[k], ans=0; rep(i, k) ile[i]=0; map<ll,ll>mp; mp[0]=1; rep(i, s.size()) { ++ile[s[i]-'a']; ll p=0, mi=M; rep(j, k) mi=min(mi, ile[j]); rep(j, k) p=p*M+ile[j]-mi; ans+=mp[p]; ++mp[p]; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; ll ans=0; for(int i=1; i<8; ++i) { string akt=""; ll k=0; rep(j, 3) if(i&(1<<j)) ++k; rep(j, s.size()) { bool ok=false; rep(l, 3) if((i&(1<<l)) && s[j]-'a'==l) ok=true; if(ok) { if(s[j]=='c') { if((i&1) && (i&2)) akt+="c"; else if((i&1) || (i&2)) akt+="b"; else akt+="a"; } else if(s[j]=='b') { if((i&1)) akt+="b"; else akt+="a"; } else akt+="a"; } else { ans+=solve(akt, k); akt=""; } } ans+=solve(akt, k); } cout << ans << '\n'; } |