#include <iostream> #include <map> using namespace std; int32_t handle(map<int32_t, int32_t> &count, int32_t shift, int dir); int32_t get(map<int32_t, int32_t> &count, int32_t shift); void increment(map<int32_t, int32_t> &count, int32_t shift); int32_t handle2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int dir1, int32_t shift2, int dir2); int32_t get2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2); void increment2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string string; cin >> string; map<int32_t, int32_t> countAB; map<int32_t, int32_t> countAC; map<int32_t, int32_t> countBC; map<pair<int32_t, int32_t>, int32_t> countABC; int32_t onlyA = 0; int32_t onlyB = 0; int32_t onlyC = 0; int32_t shiftAB = 0; int32_t shiftAC = 0; int32_t shiftBC = 0; int32_t shiftABC_B = 0; int32_t shiftABC_C = 0; long long result = 0; for (char &c : string) { result++; if (c == 'a') { result += onlyA; onlyA++; onlyB = 0; onlyC = 0; countBC.clear(); shiftAB++; shiftAC++; result += handle(countAB, shiftAB, -1); result += handle(countAC, shiftAC, -1); shiftABC_B++; shiftABC_C++; result += handle2(countABC, shiftABC_B, -1, shiftABC_C, -1); } else if (c == 'b') { result += onlyB; onlyB++; onlyA = 0; onlyC = 0; countAC.clear(); shiftAB--; shiftBC++; result += handle(countAB, shiftAB, 1); result += handle(countBC, shiftBC, -1); shiftABC_B--; result += handle2(countABC, shiftABC_B, 1, shiftABC_C, 0); } else if (c == 'c') { result += onlyC; onlyC++; onlyA = 0; onlyB = 0; countAB.clear(); shiftAC--; shiftBC--; result += handle(countAC, shiftAC, 1); result += handle(countBC, shiftBC, 1); shiftABC_C--; result += handle2(countABC, shiftABC_B, 0, shiftABC_C, 1); } } cout << result << endl; return 0; } int32_t handle(map<int32_t, int32_t> &count, int32_t shift, int dir) { increment(count, shift + dir); return get(count, shift); } void increment(map<int32_t, int32_t> &count, int32_t shift) { const map<int32_t, int32_t>::iterator &iterator = count.find(shift); if (iterator != count.end()) { iterator->second++; } else { count.insert(pair<int32_t, int32_t>(shift, 1)); } } int32_t get(map<int32_t, int32_t> &count, int32_t shift) { const map<int32_t, int32_t>::iterator &iterator = count.find(shift); if (iterator != count.end()) { return iterator->second; } else { return 0; } } int32_t handle2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int dir1, int32_t shift2, int dir2) { increment2(count, shift1 + dir1, shift2 + dir2); return get2(count, shift1, shift2); } void increment2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2) { const map<pair<int32_t, int32_t>, int32_t>::iterator &iterator = count.find(pair<int32_t, int32_t>(shift1, shift2)); if (iterator != count.end()) { iterator->second++; } else { count.insert(pair<pair<int32_t, int32_t>, int32_t>(pair<int32_t, int32_t>(shift1, shift2), 1)); } } int32_t get2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2) { const map<pair<int32_t, int32_t>, int32_t>::iterator &iterator = count.find(pair<int32_t, int32_t>(shift1, shift2)); if (iterator != count.end()) { return iterator->second; } else { 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 | #include <iostream> #include <map> using namespace std; int32_t handle(map<int32_t, int32_t> &count, int32_t shift, int dir); int32_t get(map<int32_t, int32_t> &count, int32_t shift); void increment(map<int32_t, int32_t> &count, int32_t shift); int32_t handle2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int dir1, int32_t shift2, int dir2); int32_t get2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2); void increment2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string string; cin >> string; map<int32_t, int32_t> countAB; map<int32_t, int32_t> countAC; map<int32_t, int32_t> countBC; map<pair<int32_t, int32_t>, int32_t> countABC; int32_t onlyA = 0; int32_t onlyB = 0; int32_t onlyC = 0; int32_t shiftAB = 0; int32_t shiftAC = 0; int32_t shiftBC = 0; int32_t shiftABC_B = 0; int32_t shiftABC_C = 0; long long result = 0; for (char &c : string) { result++; if (c == 'a') { result += onlyA; onlyA++; onlyB = 0; onlyC = 0; countBC.clear(); shiftAB++; shiftAC++; result += handle(countAB, shiftAB, -1); result += handle(countAC, shiftAC, -1); shiftABC_B++; shiftABC_C++; result += handle2(countABC, shiftABC_B, -1, shiftABC_C, -1); } else if (c == 'b') { result += onlyB; onlyB++; onlyA = 0; onlyC = 0; countAC.clear(); shiftAB--; shiftBC++; result += handle(countAB, shiftAB, 1); result += handle(countBC, shiftBC, -1); shiftABC_B--; result += handle2(countABC, shiftABC_B, 1, shiftABC_C, 0); } else if (c == 'c') { result += onlyC; onlyC++; onlyA = 0; onlyB = 0; countAB.clear(); shiftAC--; shiftBC--; result += handle(countAC, shiftAC, 1); result += handle(countBC, shiftBC, 1); shiftABC_C--; result += handle2(countABC, shiftABC_B, 0, shiftABC_C, 1); } } cout << result << endl; return 0; } int32_t handle(map<int32_t, int32_t> &count, int32_t shift, int dir) { increment(count, shift + dir); return get(count, shift); } void increment(map<int32_t, int32_t> &count, int32_t shift) { const map<int32_t, int32_t>::iterator &iterator = count.find(shift); if (iterator != count.end()) { iterator->second++; } else { count.insert(pair<int32_t, int32_t>(shift, 1)); } } int32_t get(map<int32_t, int32_t> &count, int32_t shift) { const map<int32_t, int32_t>::iterator &iterator = count.find(shift); if (iterator != count.end()) { return iterator->second; } else { return 0; } } int32_t handle2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int dir1, int32_t shift2, int dir2) { increment2(count, shift1 + dir1, shift2 + dir2); return get2(count, shift1, shift2); } void increment2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2) { const map<pair<int32_t, int32_t>, int32_t>::iterator &iterator = count.find(pair<int32_t, int32_t>(shift1, shift2)); if (iterator != count.end()) { iterator->second++; } else { count.insert(pair<pair<int32_t, int32_t>, int32_t>(pair<int32_t, int32_t>(shift1, shift2), 1)); } } int32_t get2(map<pair<int32_t, int32_t>, int32_t> &count, int32_t shift1, int32_t shift2) { const map<pair<int32_t, int32_t>, int32_t>::iterator &iterator = count.find(pair<int32_t, int32_t>(shift1, shift2)); if (iterator != count.end()) { return iterator->second; } else { return 0; } } |