#include <cstdint> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <unordered_map> #include <string_view> #include <functional> std::string h_str; struct count { std::uint32_t a{0}; std::uint32_t b{0}; std::uint32_t c{0}; void add(char x) { if (x == 'a') { ++a; } if (x == 'b') { ++b; } if (x == 'c') { ++c; } } void print() { std::cout << "a:" << a << " b:" << b << " c:" << c << "; "; } std::size_t hash() { h_str = "a" + std::to_string(a) + "b" + std::to_string(b) + "c" + std::to_string(c); return std::hash<std::string>{}(h_str); } bool is_balanced() const { std::vector<char> to_comp; if(a != 0) { to_comp.push_back(a); } if(b != 0) { to_comp.push_back(b); } if(c != 0) { to_comp.push_back(c); } auto prev = to_comp.begin(); bool ret = true; while(prev != to_comp.end()) { auto next = std::next(prev); if(next != to_comp.end()) { ret &= *prev == *next; } prev = next; } return ret; } }; count operator-(const count& l, const count& r) { count ct; ct.a = l.a - r.a; ct.b = l.b - r.b; ct.c = l.c - r.c; return ct; } using type = long; struct linear_ {} linear; struct dp_ {} dp; std::unordered_map<std::size_t, type> results; std::vector<count> counts; std::size_t wsize; std::string word; std::string_view sv; std::size_t key; int pos_a = 0; int pos_b = 0; int s; count ct; std::unordered_map<std::size_t, type>::iterator found; type res = 0; static type res_arr[300001]; type r_idx = 0; template<typename It> std::string_view make_sv(It begin, It end) { return std::string_view(&(*begin), std::distance(begin, end)); } type solution(const std::string& word, std::size_t idx_f, std::size_t idx_b) { if(idx_b > idx_f) { return 0; } auto sv = make_sv(word.begin()+idx_b, word.begin()+idx_f+1); auto key = std::hash<std::string_view>{}(sv); // static int a = 0; // if(a > 3000000) { // a=0; // } // auto key = std::hash<int>{}(a++); if (auto found = results.find(key); found != results.end()) { return found->second; } type res = 0; // counts[idx_f+1].print(); // std::cout << '\n'; // counts[idx_b].print(); // std::cout << '\n'; ct = counts[idx_f+1] - counts[idx_b]; // ct.print(); // std::cout << idx_b << " " << idx_f << " " << sv << " "; if (ct.is_balanced()) { // std::cout << " YES\n"; ++res; } res += solution(word, idx_f, ++idx_b); results[key] = res; return res; } type solution(const std::string& word) { type res = 0; std::size_t idx_f = 0; auto w_size = word.size(); while (idx_f < w_size) { res += solution(word, idx_f, 0); ++idx_f; } return res; } type solution() { // std::cout << indent << pos_a << " " << pos_b << '\n'; if(pos_a > wsize-1) { return 0; } if(pos_b > wsize-1) { return 0; } // std::cout << indent << "here" << '\n'; s = (int)wsize - (pos_a + pos_b); if(s <= 0) { return 0; } sv = make_sv(word.begin()+pos_a, word.end()-pos_b); auto key = std::hash<std::string_view>{}(sv); ct = counts[wsize - pos_b] - counts[pos_a]; // auto key = ct.hash(); // std::cout << indent << sign << " " << pos_a << " " << pos_b << " " << sv << " h: " << key << '\n'; // if(found = results.find(key); found != results.end()) { // // std::cout << indent << found->second << '\n'; // return found->second; // } type res = 0; // if(s < 10000) { // res = solution(word, linear); // } else { // if(s == 1) { // res = 1; // res = solution(word, linear); // } else { // auto rhs = solution(word, pos_a, pos_b+1, 1); // auto lhs = solution(word, pos_a+1, pos_b); // auto sub = lhs.first; // res = rhs.first + lhs.first - lhs.second; if (ct.is_balanced()) { // ++res_arr[r_idx]; ++res; } pos_b += 1; res += solution(); // res_arr[r_idx++] += solution(); // --r_idx; pos_b -= 1; pos_a += 1; res += solution(); // res_arr[r_idx++] += solution(); // --r_idx; pos_a -= 1; pos_a += 1; pos_b += 1; res -= solution(); // res_arr[r_idx++] -= solution(); // --r_idx; pos_a -= 1; pos_b -= 1; // std::cout << indent << "s: " << s << '\n'; // std::cout << indent << "wsize - pos_b: " << wsize - pos_b << " pos_a: " << pos_a << '\n'; // } // std::cout << indent << res << '\n'; // results[key] = res; return res; } void make_test(std::size_t size) { for(int i=0; i<size; i+=3) { std::cout << "abc"; } } void run_alg() { std::cin >> word; counts.reserve(word.size()+1); count ct; counts.push_back(ct); for (auto x : word) { ct.add(x); counts.push_back(ct); // ct.print(); // std::cout << '\n'; } // std::cout << '\n'; std::cout << solution(word) << '\n'; } int main(int argc, char** argv) { run_alg(); // char mode = argv[1][0]; // switch(mode) { // case 't': // { // std::size_t size = static_cast<std::size_t>(std::atoi(argv[2])); // make_test(size); // break; // } // case 'a': // run_alg(); // break; // } 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | #include <cstdint> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <unordered_map> #include <string_view> #include <functional> std::string h_str; struct count { std::uint32_t a{0}; std::uint32_t b{0}; std::uint32_t c{0}; void add(char x) { if (x == 'a') { ++a; } if (x == 'b') { ++b; } if (x == 'c') { ++c; } } void print() { std::cout << "a:" << a << " b:" << b << " c:" << c << "; "; } std::size_t hash() { h_str = "a" + std::to_string(a) + "b" + std::to_string(b) + "c" + std::to_string(c); return std::hash<std::string>{}(h_str); } bool is_balanced() const { std::vector<char> to_comp; if(a != 0) { to_comp.push_back(a); } if(b != 0) { to_comp.push_back(b); } if(c != 0) { to_comp.push_back(c); } auto prev = to_comp.begin(); bool ret = true; while(prev != to_comp.end()) { auto next = std::next(prev); if(next != to_comp.end()) { ret &= *prev == *next; } prev = next; } return ret; } }; count operator-(const count& l, const count& r) { count ct; ct.a = l.a - r.a; ct.b = l.b - r.b; ct.c = l.c - r.c; return ct; } using type = long; struct linear_ {} linear; struct dp_ {} dp; std::unordered_map<std::size_t, type> results; std::vector<count> counts; std::size_t wsize; std::string word; std::string_view sv; std::size_t key; int pos_a = 0; int pos_b = 0; int s; count ct; std::unordered_map<std::size_t, type>::iterator found; type res = 0; static type res_arr[300001]; type r_idx = 0; template<typename It> std::string_view make_sv(It begin, It end) { return std::string_view(&(*begin), std::distance(begin, end)); } type solution(const std::string& word, std::size_t idx_f, std::size_t idx_b) { if(idx_b > idx_f) { return 0; } auto sv = make_sv(word.begin()+idx_b, word.begin()+idx_f+1); auto key = std::hash<std::string_view>{}(sv); // static int a = 0; // if(a > 3000000) { // a=0; // } // auto key = std::hash<int>{}(a++); if (auto found = results.find(key); found != results.end()) { return found->second; } type res = 0; // counts[idx_f+1].print(); // std::cout << '\n'; // counts[idx_b].print(); // std::cout << '\n'; ct = counts[idx_f+1] - counts[idx_b]; // ct.print(); // std::cout << idx_b << " " << idx_f << " " << sv << " "; if (ct.is_balanced()) { // std::cout << " YES\n"; ++res; } res += solution(word, idx_f, ++idx_b); results[key] = res; return res; } type solution(const std::string& word) { type res = 0; std::size_t idx_f = 0; auto w_size = word.size(); while (idx_f < w_size) { res += solution(word, idx_f, 0); ++idx_f; } return res; } type solution() { // std::cout << indent << pos_a << " " << pos_b << '\n'; if(pos_a > wsize-1) { return 0; } if(pos_b > wsize-1) { return 0; } // std::cout << indent << "here" << '\n'; s = (int)wsize - (pos_a + pos_b); if(s <= 0) { return 0; } sv = make_sv(word.begin()+pos_a, word.end()-pos_b); auto key = std::hash<std::string_view>{}(sv); ct = counts[wsize - pos_b] - counts[pos_a]; // auto key = ct.hash(); // std::cout << indent << sign << " " << pos_a << " " << pos_b << " " << sv << " h: " << key << '\n'; // if(found = results.find(key); found != results.end()) { // // std::cout << indent << found->second << '\n'; // return found->second; // } type res = 0; // if(s < 10000) { // res = solution(word, linear); // } else { // if(s == 1) { // res = 1; // res = solution(word, linear); // } else { // auto rhs = solution(word, pos_a, pos_b+1, 1); // auto lhs = solution(word, pos_a+1, pos_b); // auto sub = lhs.first; // res = rhs.first + lhs.first - lhs.second; if (ct.is_balanced()) { // ++res_arr[r_idx]; ++res; } pos_b += 1; res += solution(); // res_arr[r_idx++] += solution(); // --r_idx; pos_b -= 1; pos_a += 1; res += solution(); // res_arr[r_idx++] += solution(); // --r_idx; pos_a -= 1; pos_a += 1; pos_b += 1; res -= solution(); // res_arr[r_idx++] -= solution(); // --r_idx; pos_a -= 1; pos_b -= 1; // std::cout << indent << "s: " << s << '\n'; // std::cout << indent << "wsize - pos_b: " << wsize - pos_b << " pos_a: " << pos_a << '\n'; // } // std::cout << indent << res << '\n'; // results[key] = res; return res; } void make_test(std::size_t size) { for(int i=0; i<size; i+=3) { std::cout << "abc"; } } void run_alg() { std::cin >> word; counts.reserve(word.size()+1); count ct; counts.push_back(ct); for (auto x : word) { ct.add(x); counts.push_back(ct); // ct.print(); // std::cout << '\n'; } // std::cout << '\n'; std::cout << solution(word) << '\n'; } int main(int argc, char** argv) { run_alg(); // char mode = argv[1][0]; // switch(mode) { // case 't': // { // std::size_t size = static_cast<std::size_t>(std::atoi(argv[2])); // make_test(size); // break; // } // case 'a': // run_alg(); // break; // } return 0; } |