#include <iostream> #include <string> bool is_vowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y'); } // Return true if s[pos-2:pos] (both inclusive) is all vowels or all consonants. bool is_block(std::string & s, int pos) { if (pos < 2 || pos >= s.length()) return false; return ((is_vowel(s[pos]) && is_vowel(s[pos - 1]) && is_vowel(s[pos - 2])) || (!is_vowel(s[pos]) && !is_vowel(s[pos - 1]) && !is_vowel(s[pos - 2]))); } // Dynamically. int get_blocks(std::string & s) { int num_blocks = 0; int last_block = -1; for (int i = 2; i < s.length(); i++) { // Invariant: `num_blocks` holds count of blocks in s[0:i-1]. if (is_block(s, i)) last_block = i - 2; num_blocks += last_block + 1; } return num_blocks; } int main() { // Get input. std::string inp; std::cin >> inp; std::cout << get_blocks(inp); 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 | #include <iostream> #include <string> bool is_vowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y'); } // Return true if s[pos-2:pos] (both inclusive) is all vowels or all consonants. bool is_block(std::string & s, int pos) { if (pos < 2 || pos >= s.length()) return false; return ((is_vowel(s[pos]) && is_vowel(s[pos - 1]) && is_vowel(s[pos - 2])) || (!is_vowel(s[pos]) && !is_vowel(s[pos - 1]) && !is_vowel(s[pos - 2]))); } // Dynamically. int get_blocks(std::string & s) { int num_blocks = 0; int last_block = -1; for (int i = 2; i < s.length(); i++) { // Invariant: `num_blocks` holds count of blocks in s[0:i-1]. if (is_block(s, i)) last_block = i - 2; num_blocks += last_block + 1; } return num_blocks; } int main() { // Get input. std::string inp; std::cin >> inp; std::cout << get_blocks(inp); return 0; } |