#include <iostream> #include <string> using namespace std; int dane[3][300001]; int add(int i, int word){ int res = 0, a, b, c; for(int k = i; k < word + 1; k++){ a = dane[0][k] - dane[0][k-i]; b = dane[1][k] - dane[1][k-i]; c = dane[2][k] - dane[2][k-i]; if(a == b && b == c){ res++; } else if( (a == 0 && b == c) || (b == 0 && a == c) || (c == 0 && b == a) ){ res++; } else if( (b == 0 && c == 0) || (a == 0 && c == 0) || (a == 0 && b == 0) ){ res++; } } return res; } int main(){ string word; int a, b, c; long long result = 0; cin >> word; result += 2*word.size() - 1; if(word[0] == 'a') dane[0][1] = 1; else if(word[1] == 'b') dane[1][1] = 1; else dane[2][0] = 1; for(int i = 2; i < word.size() + 1; i++){ dane[0][i] = dane[0][i-1]; dane[1][i] = dane[1][i-1]; dane[2][i] = dane[2][i-1]; if(word[i-1] == 'a') dane[0][i] += 1; else if(word[i-1] == 'b') dane[1][i] += 1; else dane[2][i] += 1; } for(int i = 3; i <= word.size(); i++){ result += add(i, word.size()); } cout << result; }
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 | #include <iostream> #include <string> using namespace std; int dane[3][300001]; int add(int i, int word){ int res = 0, a, b, c; for(int k = i; k < word + 1; k++){ a = dane[0][k] - dane[0][k-i]; b = dane[1][k] - dane[1][k-i]; c = dane[2][k] - dane[2][k-i]; if(a == b && b == c){ res++; } else if( (a == 0 && b == c) || (b == 0 && a == c) || (c == 0 && b == a) ){ res++; } else if( (b == 0 && c == 0) || (a == 0 && c == 0) || (a == 0 && b == 0) ){ res++; } } return res; } int main(){ string word; int a, b, c; long long result = 0; cin >> word; result += 2*word.size() - 1; if(word[0] == 'a') dane[0][1] = 1; else if(word[1] == 'b') dane[1][1] = 1; else dane[2][0] = 1; for(int i = 2; i < word.size() + 1; i++){ dane[0][i] = dane[0][i-1]; dane[1][i] = dane[1][i-1]; dane[2][i] = dane[2][i-1]; if(word[i-1] == 'a') dane[0][i] += 1; else if(word[i-1] == 'b') dane[1][i] += 1; else dane[2][i] += 1; } for(int i = 3; i <= word.size(); i++){ result += add(i, word.size()); } cout << result; } |