//Mateusz Piórkowski #include <iostream> #include <string> bool is_balanced(int64_t* freq){ int64_t target_freq = 0; for(int i=0; i<3; i++){ if(freq[i]){ if(target_freq==0) target_freq = freq[i]; else if(target_freq!=freq[i]) return 0; } } return 1; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string slowo; std::cin >> slowo; int64_t output=0; int64_t freq[3]; for(int i=0; i<slowo.size(); i++){ //Check all substrings freq[0]=0; freq[1]=0; freq[2]=0; for(int j=i; j<slowo.size(); j++){ freq[slowo[j]-'a']++; if(is_balanced(freq)){ output+=1; } } } std::cout << output << "\n"; }
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 | //Mateusz Piórkowski #include <iostream> #include <string> bool is_balanced(int64_t* freq){ int64_t target_freq = 0; for(int i=0; i<3; i++){ if(freq[i]){ if(target_freq==0) target_freq = freq[i]; else if(target_freq!=freq[i]) return 0; } } return 1; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string slowo; std::cin >> slowo; int64_t output=0; int64_t freq[3]; for(int i=0; i<slowo.size(); i++){ //Check all substrings freq[0]=0; freq[1]=0; freq[2]=0; for(int j=i; j<slowo.size(); j++){ freq[slowo[j]-'a']++; if(is_balanced(freq)){ output+=1; } } } std::cout << output << "\n"; } |