#include <cstdio> #include <cstring> #include <map> #include <utility> #include <cassert> #define MAX_N 300011 using namespace std; map<pair<int, int>, int> counts; map<int, int> noA, noB, noC; char s[MAX_N]; int main() { scanf("%s", s); int n = strlen(s); int db = 0; int dc = 0; int balNoA = 0; // B - C int balNoB = 0; // A - C int balNoC = 0; // A - B int justA = 0; int justB = 0; int justC = 0; long long res = 0; for (int i = 0; i < n; i++) { counts[make_pair(db, dc)]++; noA[balNoA]++; noB[balNoB]++; noC[balNoC]++; switch (s[i]) { case 'a': //3 db--; dc--; //2 noA.clear(); balNoA = 0; balNoB++; balNoC++; //1 justA++; justB = 0; justC = 0; break; case 'b': //3 db++; //2 noB.clear(); balNoB = 0; balNoA++; balNoC--; //1 justA = 0; justB++; justC = 0; break; case 'c': //3 dc++; //2 noC.clear(); balNoC = 0; balNoA--; balNoB--; //1 justA = 0; justB = 0; justC++; break; default: assert(false); } res += justA; res += justB; res += justC; res += noA[balNoA]; res += noB[balNoB]; res += noC[balNoC]; res += counts[make_pair(db, dc)]; } printf("%lld\n", res); }
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 | #include <cstdio> #include <cstring> #include <map> #include <utility> #include <cassert> #define MAX_N 300011 using namespace std; map<pair<int, int>, int> counts; map<int, int> noA, noB, noC; char s[MAX_N]; int main() { scanf("%s", s); int n = strlen(s); int db = 0; int dc = 0; int balNoA = 0; // B - C int balNoB = 0; // A - C int balNoC = 0; // A - B int justA = 0; int justB = 0; int justC = 0; long long res = 0; for (int i = 0; i < n; i++) { counts[make_pair(db, dc)]++; noA[balNoA]++; noB[balNoB]++; noC[balNoC]++; switch (s[i]) { case 'a': //3 db--; dc--; //2 noA.clear(); balNoA = 0; balNoB++; balNoC++; //1 justA++; justB = 0; justC = 0; break; case 'b': //3 db++; //2 noB.clear(); balNoB = 0; balNoA++; balNoC--; //1 justA = 0; justB++; justC = 0; break; case 'c': //3 dc++; //2 noC.clear(); balNoC = 0; balNoA--; balNoB--; //1 justA = 0; justB = 0; justC++; break; default: assert(false); } res += justA; res += justB; res += justC; res += noA[balNoA]; res += noB[balNoB]; res += noC[balNoC]; res += counts[make_pair(db, dc)]; } printf("%lld\n", res); } |