#include <stdio.h> #include <ctype.h> #include <assert.h> #define TAB_SIZE 200005 char t[TAB_SIZE]; int n; int is_vowel(char c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': return 1; default: return 0; } } int is_consonant(char c) { return !is_vowel(c); } int is_bad_block(int k) // check if all 3 following letters starting from position k are all vowels or all consonants { if (k >= n-2) { return 0; } if (is_vowel(t[k])) { if (is_consonant(t[k+1])) { return 0; } if (is_consonant(t[k+2])) { return 0; } return 1; } else { if (is_vowel(t[k+1])) { return 0; } if (is_vowel(t[k+2])) { return 0; } return 1; } } long long calc_all_subwords(long long k) // calc a number of subword of length >= 3 and <= k { return (k*k - k*3)/2 + 1; } long long calc_ok_words() // calc a number of subwords in whole table that are ok (not include bad blocks) { int l, r; long long result; l = r = 0; result = 0; while(r < n) { if (is_bad_block(r) == 0) { r++; continue; } //printf("bad block found at pos %d\n", r); result += calc_all_subwords(r - l + 2); //printf("l=%d, r=%d, res=%d\n", l, r, result); l = r + 1; r++; } result += calc_all_subwords(r - l); //printf("l=%d, r=%d, res=%d\n", l, r, result); return result; } int main() { n = fread( t, 1, TAB_SIZE, stdin ); while(islower(t[n-1]) == 0) { n--; } if (n <= 2) { printf("0\n"); } else { printf("%lld\n", calc_all_subwords(n) - calc_ok_words()); } 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <stdio.h> #include <ctype.h> #include <assert.h> #define TAB_SIZE 200005 char t[TAB_SIZE]; int n; int is_vowel(char c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'y': return 1; default: return 0; } } int is_consonant(char c) { return !is_vowel(c); } int is_bad_block(int k) // check if all 3 following letters starting from position k are all vowels or all consonants { if (k >= n-2) { return 0; } if (is_vowel(t[k])) { if (is_consonant(t[k+1])) { return 0; } if (is_consonant(t[k+2])) { return 0; } return 1; } else { if (is_vowel(t[k+1])) { return 0; } if (is_vowel(t[k+2])) { return 0; } return 1; } } long long calc_all_subwords(long long k) // calc a number of subword of length >= 3 and <= k { return (k*k - k*3)/2 + 1; } long long calc_ok_words() // calc a number of subwords in whole table that are ok (not include bad blocks) { int l, r; long long result; l = r = 0; result = 0; while(r < n) { if (is_bad_block(r) == 0) { r++; continue; } //printf("bad block found at pos %d\n", r); result += calc_all_subwords(r - l + 2); //printf("l=%d, r=%d, res=%d\n", l, r, result); l = r + 1; r++; } result += calc_all_subwords(r - l); //printf("l=%d, r=%d, res=%d\n", l, r, result); return result; } int main() { n = fread( t, 1, TAB_SIZE, stdin ); while(islower(t[n-1]) == 0) { n--; } if (n <= 2) { printf("0\n"); } else { printf("%lld\n", calc_all_subwords(n) - calc_ok_words()); } return 0; } |