#include <iostream> using namespace std; bool is_balanced(string word) { char chars[256] = {0}; for (int i = 0; i < word.size(); i++) { chars[word[i]]++; } for (int i = 0; i < 256; i++) { for (int j = i + 1; j < 256; j++) { if(chars[i] != 0 && chars[j] != 0 && chars[i] != chars[j]) { return false; } } } return true; } int main() { string word; cin >> word; int count = 0; for (int i = 0; i < word.size(); i++) { for (int j = i; j < word.size(); j++) { string new_word = string(&word[i], &word[j] + 1); if(is_balanced(new_word)) { count++; } } } cout << count; 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 | #include <iostream> using namespace std; bool is_balanced(string word) { char chars[256] = {0}; for (int i = 0; i < word.size(); i++) { chars[word[i]]++; } for (int i = 0; i < 256; i++) { for (int j = i + 1; j < 256; j++) { if(chars[i] != 0 && chars[j] != 0 && chars[i] != chars[j]) { return false; } } } return true; } int main() { string word; cin >> word; int count = 0; for (int i = 0; i < word.size(); i++) { for (int j = i; j < word.size(); j++) { string new_word = string(&word[i], &word[j] + 1); if(is_balanced(new_word)) { count++; } } } cout << count; return 0; } |