#include <iostream> const int MAX_N = 300001; int cnt[MAX_N][3]; #define INDEX(x) (x-'a') #define REP(x,n) for(int x=0;x<(n);++x) #ifdef _HOME_ #define DEBUG(x) cerr<<x #else #define DEBUG(x) #endif // _HOME_ using namespace std; bool equal(int a, int b, int c) { if (!a) { if (!b) { return true; } return !c || b==c; } return (!b || b==a) && (!c || c==a); } bool equal(int* arr) { return equal(arr[0], arr[1], arr[2]); } int diff(int index, int start, int end) { return cnt[end][index] - cnt[start][index]; } int main() { ios_base::sync_with_stdio(0); #ifdef _HOME_ string str = "aabbabcccba"; #else string str; cin>>str; #endif int n = str.size(); REP(x,n) { cnt[x+1][0] = cnt[x][0]; cnt[x+1][1] = cnt[x][1]; cnt[x+1][2] = cnt[x][2]; ++cnt[x+1][INDEX(str[x])]; } #ifdef _HOME_ for(int i=0;i<3;++i) { REP(x,n) DEBUG(cnt[x][i]<<" "); DEBUG(endl); } #endif long long result = n + (n-1); REP(end,n+1) { REP(start,end-2) { if (equal(diff(0, start, end), diff(1, start, end), diff(2, start, end))) { ++result; DEBUG(result << "\t" << str.substr(start, end) << endl); } } } cout << result << endl; 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 | #include <iostream> const int MAX_N = 300001; int cnt[MAX_N][3]; #define INDEX(x) (x-'a') #define REP(x,n) for(int x=0;x<(n);++x) #ifdef _HOME_ #define DEBUG(x) cerr<<x #else #define DEBUG(x) #endif // _HOME_ using namespace std; bool equal(int a, int b, int c) { if (!a) { if (!b) { return true; } return !c || b==c; } return (!b || b==a) && (!c || c==a); } bool equal(int* arr) { return equal(arr[0], arr[1], arr[2]); } int diff(int index, int start, int end) { return cnt[end][index] - cnt[start][index]; } int main() { ios_base::sync_with_stdio(0); #ifdef _HOME_ string str = "aabbabcccba"; #else string str; cin>>str; #endif int n = str.size(); REP(x,n) { cnt[x+1][0] = cnt[x][0]; cnt[x+1][1] = cnt[x][1]; cnt[x+1][2] = cnt[x][2]; ++cnt[x+1][INDEX(str[x])]; } #ifdef _HOME_ for(int i=0;i<3;++i) { REP(x,n) DEBUG(cnt[x][i]<<" "); DEBUG(endl); } #endif long long result = n + (n-1); REP(end,n+1) { REP(start,end-2) { if (equal(diff(0, start, end), diff(1, start, end), diff(2, start, end))) { ++result; DEBUG(result << "\t" << str.substr(start, end) << endl); } } } cout << result << endl; return 0; } |