#include <cstdio> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define SD second #define LINE(n,x) char x[n]; fgets(x, n, stdin) typedef long long LL; typedef vector<int> VI; int main() { LINE(300010, s); LL r = 0; FOR(ma,1,8) { map<VI, int> m; VI v(3); ++m[v]; for (char *c = s; *c >= 'a' && *c <= 'c'; ++c) { ++v[*c - 'a']; int mi = 1000000000; REP(i,3) if (ma & (1 << i)) mi = min(mi, v[i]); REP(i,3) if (ma & (1 << i)) v[i] -= mi; ++m[v]; } FORE(it,m) r += LL(it->SD) * (it->SD - 1) >> 1; } printf("%lld\n", r); }
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 | #include <cstdio> #include <map> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define SD second #define LINE(n,x) char x[n]; fgets(x, n, stdin) typedef long long LL; typedef vector<int> VI; int main() { LINE(300010, s); LL r = 0; FOR(ma,1,8) { map<VI, int> m; VI v(3); ++m[v]; for (char *c = s; *c >= 'a' && *c <= 'c'; ++c) { ++v[*c - 'a']; int mi = 1000000000; REP(i,3) if (ma & (1 << i)) mi = min(mi, v[i]); REP(i,3) if (ma & (1 << i)) v[i] -= mi; ++m[v]; } FORE(it,m) r += LL(it->SD) * (it->SD - 1) >> 1; } printf("%lld\n", r); } |