#include <iostream> #include <cstdio> #include <cstdint> #include <cinttypes> #include <cstring> #include <vector> #include <string> #include <map> #include <set> using namespace std; uint64_t solve_brut(const char *s) { const uint64_t n = strlen(s); int exists[3]; memset(exists, 0, sizeof(exists)); static int cnt[300300][3]; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) { if (i > 0) { for (int j = 0; j < 3; j++) { cnt[i][j] = cnt[i - 1][j]; } } cnt[i][s[i] - 'a']++; exists[s[i] - 'a'] = 1; } uint64_t res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { vector<int> diff(3); // number of a, b, c in the substring i..j for (int k = 0; k < 3; k++) { diff[k] = cnt[j][k]; if (i > 0) diff[k] -= cnt[i - 1][k]; } if ((diff[0] == diff[1] || diff[0] == 0 || diff[1] == 0) && (diff[1] == diff[2] || diff[1] == 0 || diff[2] == 0) && (diff[2] == diff[0] || diff[2] == 0 || diff[0] == 0)) { res++; } } } return res; } int main() { static char s[300300]; scanf("%s", s); uint64_t res = solve_brut(s); printf("%" PRIu64 "\n", res); 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 | #include <iostream> #include <cstdio> #include <cstdint> #include <cinttypes> #include <cstring> #include <vector> #include <string> #include <map> #include <set> using namespace std; uint64_t solve_brut(const char *s) { const uint64_t n = strlen(s); int exists[3]; memset(exists, 0, sizeof(exists)); static int cnt[300300][3]; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) { if (i > 0) { for (int j = 0; j < 3; j++) { cnt[i][j] = cnt[i - 1][j]; } } cnt[i][s[i] - 'a']++; exists[s[i] - 'a'] = 1; } uint64_t res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { vector<int> diff(3); // number of a, b, c in the substring i..j for (int k = 0; k < 3; k++) { diff[k] = cnt[j][k]; if (i > 0) diff[k] -= cnt[i - 1][k]; } if ((diff[0] == diff[1] || diff[0] == 0 || diff[1] == 0) && (diff[1] == diff[2] || diff[1] == 0 || diff[2] == 0) && (diff[2] == diff[0] || diff[2] == 0 || diff[0] == 0)) { res++; } } } return res; } int main() { static char s[300300]; scanf("%s", s); uint64_t res = solve_brut(s); printf("%" PRIu64 "\n", res); return 0; } |