#include<cstdio> char * s; int len; int * t; // t[i] = position of the end of the first triplet to the right of i void input() { s = new char[200005]; t = new int[200005]; scanf("%s", s); len = 0; while(s[len] != '\0') { len++; } } int type(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { return 1; } return 0; } void solve_slow() { long long res = 0; for(int i = 0; i < len - 2; i++) { char curr = s[i]; int count = 1; for(int j = i + 1; j < len; j++) { if (type(curr) == type(s[j])) { count ++; if (count == 3) { res += (len - j); break; } } else { count = 1; } curr = s[j]; } } printf("%lld\n", res); } void generate_lookup() { t[len - 1] = len; char curr = s[len - 1]; int count = 1; for (int i = len - 2; i >= 0; i--) { if (type(curr) == type(s[i])) { count++; if (count >= 3) { t[i] = i + 2; } else { t[i] = t[i + 1]; } } else { count = 1; t[i] = t[i + 1]; } curr = s[i]; } } void print_lookup() { for (int i = 0; i < len; i++) { printf("%d ", t[i]); } printf("\n"); } void solve_fast() { generate_lookup(); //print_lookup(); long long res = 0; for(int i = 0; i < len; i++) { res += (len - t[i]); } printf("%lld\n", res); } int main(int argc, char ** argv) { input(); solve_fast(); 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 | #include<cstdio> char * s; int len; int * t; // t[i] = position of the end of the first triplet to the right of i void input() { s = new char[200005]; t = new int[200005]; scanf("%s", s); len = 0; while(s[len] != '\0') { len++; } } int type(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { return 1; } return 0; } void solve_slow() { long long res = 0; for(int i = 0; i < len - 2; i++) { char curr = s[i]; int count = 1; for(int j = i + 1; j < len; j++) { if (type(curr) == type(s[j])) { count ++; if (count == 3) { res += (len - j); break; } } else { count = 1; } curr = s[j]; } } printf("%lld\n", res); } void generate_lookup() { t[len - 1] = len; char curr = s[len - 1]; int count = 1; for (int i = len - 2; i >= 0; i--) { if (type(curr) == type(s[i])) { count++; if (count >= 3) { t[i] = i + 2; } else { t[i] = t[i + 1]; } } else { count = 1; t[i] = t[i + 1]; } curr = s[i]; } } void print_lookup() { for (int i = 0; i < len; i++) { printf("%d ", t[i]); } printf("\n"); } void solve_fast() { generate_lookup(); //print_lookup(); long long res = 0; for(int i = 0; i < len; i++) { res += (len - t[i]); } printf("%lld\n", res); } int main(int argc, char ** argv) { input(); solve_fast(); return 0; } |