#include <unordered_map> #include <vector> #include <cstring> #include <cstdio> #define ll long long const ll BASE = 1 << 26; using namespace std; class PrevDiffs { unordered_map<ll, int> cmap; ll res = 0; public: PrevDiffs(ll zero) { cmap[zero] = 1; } ll getRes() { return res; } void update(ll x) { auto it = cmap.find(x); if(it != cmap.end()) { res += it->second; cmap[x] = (it->second) + 1; } else { cmap[x] = 1; } } }; class Solver { char* a; int n; public: Solver(char* a, int n): a(a), n(n) {} ll solve() { ll res = 0; res += countSingleLetter(); res += countTwoLetters(); res += countThreeLetters(); return res; } private: ll countSingleLetter() { ll res = 0; ll start = 0; for(int i=0;i<=n;i++) { if((i == n) || ((i > 0) && (a[i] != a[i-1]))) { res += ((i - start) * (i - start + 1)) / 2; start = i; } } return res; } ll countTwoLetters() { ll res = 0; vector<int> start(3, -1); vector<int> last(3, -1); int cnt[n+1][3]; fill(cnt[0], cnt[0] + 3, 0); vector<int> biggest(2, -1); vector<int> nbiggest(2, -1); PrevDiffs* pd = new PrevDiffs(0); int c = 0; int base = 0; for(int i=0;i<n;i++) { int r = a[i] - 'a'; if((i == 0) || (a[i] != a[i-1])) { start[r] = i; } last[r] = i; memcpy(cnt[i+1], cnt[i], sizeof(int) * 3); cnt[i+1][r]++; if(c < 3) { c = 0; for(auto &x : last) { if(x != -1) { c++; } } } if(c >= 2) { chooseBiggest(last, nbiggest, c); if(biggest != nbiggest) { biggest = nbiggest; int from = start[a[i-1]-'a']; base = cnt[from][biggest[0]] - cnt[from][biggest[1]]; res += pd->getRes(); delete pd; pd = new PrevDiffs(0); for(int k=from+1;k<=i;k++) { pd->update(cnt[k][biggest[0]] - cnt[k][biggest[1]] - base); } } pd->update(cnt[i+1][biggest[0]] - cnt[i+1][biggest[1]] - base); } } res += pd->getRes(); return res; } void chooseBiggest(vector<int>& last, vector<int>& biggest, int c) { int smallest = -1; if(c != 2) { smallest = 0; for(int k=1;k<3;k++) { if((last[k] != -1) && (last[k] < last[smallest])) { smallest = k; } } } int p = 0; for(int k=0;k<3;k++) { if((last[k] != -1) && (k != smallest)) { biggest[p] = k; p++; } } } ll countThreeLetters() { PrevDiffs pd(BASE << 32); int cnt[3] = {}; for(int i=0;i<n;i++) { cnt[a[i]-'a']++; pd.update(((BASE + cnt[0] - cnt[1]) << 32) + cnt[0] - cnt[2]); } return pd.getRes(); } }; int main(int argc, char** argv) { int n = 0; char a[300123]; scanf("%s", a); n = strlen(a); Solver solver(a, n); ll res = solver.solve(); printf("%lld\n", res); 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | #include <unordered_map> #include <vector> #include <cstring> #include <cstdio> #define ll long long const ll BASE = 1 << 26; using namespace std; class PrevDiffs { unordered_map<ll, int> cmap; ll res = 0; public: PrevDiffs(ll zero) { cmap[zero] = 1; } ll getRes() { return res; } void update(ll x) { auto it = cmap.find(x); if(it != cmap.end()) { res += it->second; cmap[x] = (it->second) + 1; } else { cmap[x] = 1; } } }; class Solver { char* a; int n; public: Solver(char* a, int n): a(a), n(n) {} ll solve() { ll res = 0; res += countSingleLetter(); res += countTwoLetters(); res += countThreeLetters(); return res; } private: ll countSingleLetter() { ll res = 0; ll start = 0; for(int i=0;i<=n;i++) { if((i == n) || ((i > 0) && (a[i] != a[i-1]))) { res += ((i - start) * (i - start + 1)) / 2; start = i; } } return res; } ll countTwoLetters() { ll res = 0; vector<int> start(3, -1); vector<int> last(3, -1); int cnt[n+1][3]; fill(cnt[0], cnt[0] + 3, 0); vector<int> biggest(2, -1); vector<int> nbiggest(2, -1); PrevDiffs* pd = new PrevDiffs(0); int c = 0; int base = 0; for(int i=0;i<n;i++) { int r = a[i] - 'a'; if((i == 0) || (a[i] != a[i-1])) { start[r] = i; } last[r] = i; memcpy(cnt[i+1], cnt[i], sizeof(int) * 3); cnt[i+1][r]++; if(c < 3) { c = 0; for(auto &x : last) { if(x != -1) { c++; } } } if(c >= 2) { chooseBiggest(last, nbiggest, c); if(biggest != nbiggest) { biggest = nbiggest; int from = start[a[i-1]-'a']; base = cnt[from][biggest[0]] - cnt[from][biggest[1]]; res += pd->getRes(); delete pd; pd = new PrevDiffs(0); for(int k=from+1;k<=i;k++) { pd->update(cnt[k][biggest[0]] - cnt[k][biggest[1]] - base); } } pd->update(cnt[i+1][biggest[0]] - cnt[i+1][biggest[1]] - base); } } res += pd->getRes(); return res; } void chooseBiggest(vector<int>& last, vector<int>& biggest, int c) { int smallest = -1; if(c != 2) { smallest = 0; for(int k=1;k<3;k++) { if((last[k] != -1) && (last[k] < last[smallest])) { smallest = k; } } } int p = 0; for(int k=0;k<3;k++) { if((last[k] != -1) && (k != smallest)) { biggest[p] = k; p++; } } } ll countThreeLetters() { PrevDiffs pd(BASE << 32); int cnt[3] = {}; for(int i=0;i<n;i++) { cnt[a[i]-'a']++; pd.update(((BASE + cnt[0] - cnt[1]) << 32) + cnt[0] - cnt[2]); } return pd.getRes(); } }; int main(int argc, char** argv) { int n = 0; char a[300123]; scanf("%s", a); n = strlen(a); Solver solver(a, n); ll res = solver.solve(); printf("%lld\n", res); return 0; } |