#include <iostream> #include <unordered_map> using namespace std; int main() { string word; cin>>word; unsigned long long sum = word.size() + word.size() - 1;//razem dla podslow dlugosci 1 i 2 for(int i = 3; i <= word.size(); i++) { unordered_map<char, int> tab; for(int j = 0; j < i; j++) { tab[word[j]]++; } int previous_word_first_index = -1; for(int j = 0; word.size() - j >= i; j++) { if(previous_word_first_index != -1) { tab[word[previous_word_first_index]]--; tab[word[j + i - 1]]++; } bool is_balanced = true; int cnt = -1; unordered_map<char, int>::iterator it; for(it = tab.begin(); it!= tab.end(); it++) { if(it->second != 0) { if(cnt == -1) { cnt = it->second; } else if(it->second != cnt) { is_balanced = false; break; } } } sum += is_balanced; previous_word_first_index++; } } 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <unordered_map> using namespace std; int main() { string word; cin>>word; unsigned long long sum = word.size() + word.size() - 1;//razem dla podslow dlugosci 1 i 2 for(int i = 3; i <= word.size(); i++) { unordered_map<char, int> tab; for(int j = 0; j < i; j++) { tab[word[j]]++; } int previous_word_first_index = -1; for(int j = 0; word.size() - j >= i; j++) { if(previous_word_first_index != -1) { tab[word[previous_word_first_index]]--; tab[word[j + i - 1]]++; } bool is_balanced = true; int cnt = -1; unordered_map<char, int>::iterator it; for(it = tab.begin(); it!= tab.end(); it++) { if(it->second != 0) { if(cnt == -1) { cnt = it->second; } else if(it->second != cnt) { is_balanced = false; break; } } } sum += is_balanced; previous_word_first_index++; } } cout<<sum; return 0; } |