#include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_map> #include <algorithm> using namespace std; struct CharCnt { int aCnt[3]; int64_t GetKey() { int iMin = min(aCnt[0], min(aCnt[1], aCnt[2])); return ((int64_t) aCnt[0] - iMin) | (((int64_t) aCnt[1] - iMin) << 19) | (((int64_t) aCnt[2] - iMin) << 38); } }; static int64_t iTotalCnt; static string sInput; static CharCnt cntCurrent; static unordered_map<int64_t, int> mapTriples; static unordered_map<int, int> mapAB; static unordered_map<int, int> mapBC; static unordered_map<int, int> mapAC; static int iNoC_A_Cnt; static int iNoC_B_Cnt; static int iNoB_A_Cnt; static int iNoB_C_Cnt; static int iNoA_B_Cnt; static int iNoA_C_Cnt; static void Init() { iTotalCnt = 0; cntCurrent.aCnt[0] = 0; cntCurrent.aCnt[1] = 0; cntCurrent.aCnt[2] = 0; mapTriples[cntCurrent.GetKey()] = 1; iNoC_A_Cnt = 0; iNoC_B_Cnt = 0; iNoB_A_Cnt = 0; iNoB_C_Cnt = 0; iNoA_B_Cnt = 0; iNoA_C_Cnt = 0; mapAB[0] = 1; mapBC[0] = 1; mapAC[0] = 1; } static inline void CountBisignalWords(char c) { switch (c) { case 'a': mapBC.clear(); mapBC[0] = 1; iNoA_B_Cnt = 0; iNoA_C_Cnt = 0; iNoB_A_Cnt++; iNoC_A_Cnt++; iTotalCnt += mapAB[iNoC_A_Cnt - iNoC_B_Cnt]++; iTotalCnt += mapAC[iNoB_A_Cnt - iNoB_C_Cnt]++; break; case 'b': mapAC.clear(); mapAC[0] = 1; iNoB_A_Cnt = 0; iNoB_C_Cnt = 0; iNoA_B_Cnt++; iNoC_B_Cnt++; iTotalCnt += mapAB[iNoC_A_Cnt - iNoC_B_Cnt]++; iTotalCnt += mapBC[iNoA_B_Cnt - iNoA_C_Cnt]++; break; default: mapAB.clear(); mapAB[0] = 1; iNoC_A_Cnt = 0; iNoC_B_Cnt = 0; iNoA_C_Cnt++; iNoB_C_Cnt++; iTotalCnt += mapAC[iNoB_A_Cnt - iNoB_C_Cnt]++; iTotalCnt += mapBC[iNoA_B_Cnt - iNoA_C_Cnt]++; } } static inline void CountTrisignalWords(char c) { cntCurrent.aCnt[c - 'a']++; iTotalCnt += mapTriples[cntCurrent.GetKey()]++; } static void Solve() { char cPrev = 'd'; int iSameAsPrevCnt = 0; for (char c : sInput) { iTotalCnt++; if (c == cPrev) { iSameAsPrevCnt++; iTotalCnt += iSameAsPrevCnt; } else { iSameAsPrevCnt = 0; cPrev = c; } CountBisignalWords(c); CountTrisignalWords(c); } } int main(int argc, char * argv[]) { cin >> sInput; Init(); Solve(); cout << iTotalCnt; 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 163 164 165 166 167 168 | #include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_map> #include <algorithm> using namespace std; struct CharCnt { int aCnt[3]; int64_t GetKey() { int iMin = min(aCnt[0], min(aCnt[1], aCnt[2])); return ((int64_t) aCnt[0] - iMin) | (((int64_t) aCnt[1] - iMin) << 19) | (((int64_t) aCnt[2] - iMin) << 38); } }; static int64_t iTotalCnt; static string sInput; static CharCnt cntCurrent; static unordered_map<int64_t, int> mapTriples; static unordered_map<int, int> mapAB; static unordered_map<int, int> mapBC; static unordered_map<int, int> mapAC; static int iNoC_A_Cnt; static int iNoC_B_Cnt; static int iNoB_A_Cnt; static int iNoB_C_Cnt; static int iNoA_B_Cnt; static int iNoA_C_Cnt; static void Init() { iTotalCnt = 0; cntCurrent.aCnt[0] = 0; cntCurrent.aCnt[1] = 0; cntCurrent.aCnt[2] = 0; mapTriples[cntCurrent.GetKey()] = 1; iNoC_A_Cnt = 0; iNoC_B_Cnt = 0; iNoB_A_Cnt = 0; iNoB_C_Cnt = 0; iNoA_B_Cnt = 0; iNoA_C_Cnt = 0; mapAB[0] = 1; mapBC[0] = 1; mapAC[0] = 1; } static inline void CountBisignalWords(char c) { switch (c) { case 'a': mapBC.clear(); mapBC[0] = 1; iNoA_B_Cnt = 0; iNoA_C_Cnt = 0; iNoB_A_Cnt++; iNoC_A_Cnt++; iTotalCnt += mapAB[iNoC_A_Cnt - iNoC_B_Cnt]++; iTotalCnt += mapAC[iNoB_A_Cnt - iNoB_C_Cnt]++; break; case 'b': mapAC.clear(); mapAC[0] = 1; iNoB_A_Cnt = 0; iNoB_C_Cnt = 0; iNoA_B_Cnt++; iNoC_B_Cnt++; iTotalCnt += mapAB[iNoC_A_Cnt - iNoC_B_Cnt]++; iTotalCnt += mapBC[iNoA_B_Cnt - iNoA_C_Cnt]++; break; default: mapAB.clear(); mapAB[0] = 1; iNoC_A_Cnt = 0; iNoC_B_Cnt = 0; iNoA_C_Cnt++; iNoB_C_Cnt++; iTotalCnt += mapAC[iNoB_A_Cnt - iNoB_C_Cnt]++; iTotalCnt += mapBC[iNoA_B_Cnt - iNoA_C_Cnt]++; } } static inline void CountTrisignalWords(char c) { cntCurrent.aCnt[c - 'a']++; iTotalCnt += mapTriples[cntCurrent.GetKey()]++; } static void Solve() { char cPrev = 'd'; int iSameAsPrevCnt = 0; for (char c : sInput) { iTotalCnt++; if (c == cPrev) { iSameAsPrevCnt++; iTotalCnt += iSameAsPrevCnt; } else { iSameAsPrevCnt = 0; cPrev = c; } CountBisignalWords(c); CountTrisignalWords(c); } } int main(int argc, char * argv[]) { cin >> sInput; Init(); Solve(); cout << iTotalCnt; return 0; } |