#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, z, m, d, p; ll tb[300001][3]; ll ta[300001][8]; ll tc[300001][8]; vi vk, va, vb; string w; pii por(int a, int b) { pii p0 = MP(0, 0); ll td[3]; CL(td, 0); for (int j = 0; j < 3; j++) { td[j] = tb[b][j] - tb[a][j]; } for (int j = 0; j < 3; j++) { if (td[j] > 0) p0.first |= (1 << j); } if (p0.first == 7) p0.second = max(abs(td[1] - td[0]), max(abs(td[2] - td[1]), abs(td[0] - td[2]))); else if (p0.first == 3) p0.second = abs(td[1] - td[0]); else if (p0.first == 5) p0.second = abs(td[2] - td[0]); else if (p0.first == 6) p0.second = abs(td[2] - td[1]); else p0.second = max(td[0], max(td[1], td[2])); return p0; } ll maxpor(int a, int b) { ll c = 0; for (int j = 0; j < 3; j++) c = max(c, tb[b][j] - tb[a][j]); return c; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in", "rt", stdin); // while (1) { cin >> w; CL(ta, 0); CL(tb, 0); CL(tc, -1); n = w.size(); z = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (j == (w[i - 1] - 'a')) tb[i][j] = tb[i - 1][j] + 1; else tb[i][j] = tb[i - 1][j]; } } for (int i = 1; i <= n; i++) { int a = i - 1; ta[i][1 << (w[i - 1] - 'a')] = ta[i-1][1 << (w[i - 1] - 'a')] + 1; z += ta[i][1 << (w[i - 1] - 'a')]; c = ta[i][1 << (w[i - 1] - 'a')]; if (i - 2 * c >= 0) { d = 1; for (int a = i - 2 * c; a >= 0; a -= 2) { pii p1 = por(a, i); if (p1.first < 7) { if (p1.second == 0) { ta[i][p1.first] = ta[a][p1.first] + 1; z += ta[i][p1.first]; if (tc[a][p1.first] == -1) //tc[i] = i-(2 * ta[i][p1.first]); tc[i][p1.first] = a; else tc[i][p1.first] = tc[a][p1.first]; d = i-(tc[i][p1.first] + (i - tc[i][p1.first]) % 3); d /= 3; //d = maxpor(a, i); break; } } else { a += (i - a) % 3; // a = 3 * ((i - a) / 2 - 1); // d = maxpor(i - a, i); d = maxpor(a, i); break; } } c = (3 * d); // c = 3; for (int a = i - c; a >= 0; a -= 3) { pii p1 = por(a, i); if (p1.first == 7) { if (p1.second == 0) { ta[i][p1.first] = ta[a][p1.first] + 1; z += ta[i][p1.first]; break; } d = maxpor(a, i); c = 3 * d; a = i - c + 3; } else //break; continue; } } } /* for (int i = 1; i <= n; i++) { for (int j = 1; j < 8; j++) z += ta[i][j]; } */ cout << z << 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 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 151 152 153 154 155 156 157 158 159 160 161 162 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll n, k, a, b, c, z, m, d, p; ll tb[300001][3]; ll ta[300001][8]; ll tc[300001][8]; vi vk, va, vb; string w; pii por(int a, int b) { pii p0 = MP(0, 0); ll td[3]; CL(td, 0); for (int j = 0; j < 3; j++) { td[j] = tb[b][j] - tb[a][j]; } for (int j = 0; j < 3; j++) { if (td[j] > 0) p0.first |= (1 << j); } if (p0.first == 7) p0.second = max(abs(td[1] - td[0]), max(abs(td[2] - td[1]), abs(td[0] - td[2]))); else if (p0.first == 3) p0.second = abs(td[1] - td[0]); else if (p0.first == 5) p0.second = abs(td[2] - td[0]); else if (p0.first == 6) p0.second = abs(td[2] - td[1]); else p0.second = max(td[0], max(td[1], td[2])); return p0; } ll maxpor(int a, int b) { ll c = 0; for (int j = 0; j < 3; j++) c = max(c, tb[b][j] - tb[a][j]); return c; } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t1.in", "rt", stdin); // while (1) { cin >> w; CL(ta, 0); CL(tb, 0); CL(tc, -1); n = w.size(); z = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { if (j == (w[i - 1] - 'a')) tb[i][j] = tb[i - 1][j] + 1; else tb[i][j] = tb[i - 1][j]; } } for (int i = 1; i <= n; i++) { int a = i - 1; ta[i][1 << (w[i - 1] - 'a')] = ta[i-1][1 << (w[i - 1] - 'a')] + 1; z += ta[i][1 << (w[i - 1] - 'a')]; c = ta[i][1 << (w[i - 1] - 'a')]; if (i - 2 * c >= 0) { d = 1; for (int a = i - 2 * c; a >= 0; a -= 2) { pii p1 = por(a, i); if (p1.first < 7) { if (p1.second == 0) { ta[i][p1.first] = ta[a][p1.first] + 1; z += ta[i][p1.first]; if (tc[a][p1.first] == -1) //tc[i] = i-(2 * ta[i][p1.first]); tc[i][p1.first] = a; else tc[i][p1.first] = tc[a][p1.first]; d = i-(tc[i][p1.first] + (i - tc[i][p1.first]) % 3); d /= 3; //d = maxpor(a, i); break; } } else { a += (i - a) % 3; // a = 3 * ((i - a) / 2 - 1); // d = maxpor(i - a, i); d = maxpor(a, i); break; } } c = (3 * d); // c = 3; for (int a = i - c; a >= 0; a -= 3) { pii p1 = por(a, i); if (p1.first == 7) { if (p1.second == 0) { ta[i][p1.first] = ta[a][p1.first] + 1; z += ta[i][p1.first]; break; } d = maxpor(a, i); c = 3 * d; a = i - c + 3; } else //break; continue; } } } /* for (int i = 1; i <= n; i++) { for (int j = 1; j < 8; j++) z += ta[i][j]; } */ cout << z << endl; // } return 0; } |