#pragma GCC optimize ("O3") #include <iostream> #include <cstring> using namespace std; bool valid(int counter[3]) { int last = counter[0]; for (int i = 1; i < 3; i++) { if (counter[i] == 0) continue; if (last == 0) { last = counter[i]; } else if (counter[i] != last) { return false; } } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); string text; cin >> text; int n = text.length(); int counter[3]; int sum = n * 2 - 1; for (int i = 0; i < n - 2; i++) { memset(counter, 0, sizeof(counter)); for (int r = i; r < i + 3; r++) { counter[text[r] - 97]++; } for (int j = 3; j < n - i + 1; j++) { if (valid(counter)) { sum++; } counter[text[j + i] - 97]++; } } cout << sum; 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 | #pragma GCC optimize ("O3") #include <iostream> #include <cstring> using namespace std; bool valid(int counter[3]) { int last = counter[0]; for (int i = 1; i < 3; i++) { if (counter[i] == 0) continue; if (last == 0) { last = counter[i]; } else if (counter[i] != last) { return false; } } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); string text; cin >> text; int n = text.length(); int counter[3]; int sum = n * 2 - 1; for (int i = 0; i < n - 2; i++) { memset(counter, 0, sizeof(counter)); for (int r = i; r < i + 3; r++) { counter[text[r] - 97]++; } for (int j = 3; j < n - i + 1; j++) { if (valid(counter)) { sum++; } counter[text[j + i] - 97]++; } } cout << sum; return 0; } |