#include <iostream> #include <string> using namespace std; bool balans(string text) { int a=0, b=0, c=0; int l = (int) text.length(); for(int i = 0; i < l; i++) { if(text[i] == 'a') a++; else if(text[i] == 'b') b++; else c++; } if(c == 0) { if((b == 0) || (a == 0)) return true; else if(b == a) return true; else return false; } else if(b == 0) { if((c == 0) || (a == 0)) return true; else if(c == a) return true; else return false; } else if(a == 0) { if((c == 0) || (b == 0)) return true; else if(c == b) return true; else return false; } else { if((a==b)&&(a==c)) return true; } return false; } int main() { string slowo, podslowo; int a = 0, b = 0, c = 0; int odp; cin >> slowo; int ll = (int) slowo.length(); odp = ll + (ll-1); if(odp > 3) { for(int j = 3; j <= ll; j++) { for(int i = 0; i <= ll-j; i++) { podslowo = slowo.substr(i, j); odp += balans(podslowo); } } } cout << odp; 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 | #include <iostream> #include <string> using namespace std; bool balans(string text) { int a=0, b=0, c=0; int l = (int) text.length(); for(int i = 0; i < l; i++) { if(text[i] == 'a') a++; else if(text[i] == 'b') b++; else c++; } if(c == 0) { if((b == 0) || (a == 0)) return true; else if(b == a) return true; else return false; } else if(b == 0) { if((c == 0) || (a == 0)) return true; else if(c == a) return true; else return false; } else if(a == 0) { if((c == 0) || (b == 0)) return true; else if(c == b) return true; else return false; } else { if((a==b)&&(a==c)) return true; } return false; } int main() { string slowo, podslowo; int a = 0, b = 0, c = 0; int odp; cin >> slowo; int ll = (int) slowo.length(); odp = ll + (ll-1); if(odp > 3) { for(int j = 3; j <= ll; j++) { for(int i = 0; i <= ll-j; i++) { podslowo = slowo.substr(i, j); odp += balans(podslowo); } } } cout << odp; return 0; } |