#include <iostream> #include <set> #include <vector> #include <map> #include <unordered_map> using namespace std; constexpr int kConsonant = 1; constexpr int kVowel = -1; bool isThreeInRow(vector<bool> &V) { if (V.size() < 3) return false; for (int i = V.size() - 3;i < V.size() - 1;i++) if (V[i] != V[i + 1]) return false; return true; } int main() { ios_base::sync_with_stdio(0); map<char, int> characters = { {'a', kVowel}, {'b', kConsonant}, {'c', kConsonant}, {'d', kConsonant}, {'e', kVowel}, {'f', kConsonant}, {'g', kConsonant}, {'h', kConsonant}, {'i', kVowel}, {'j', kConsonant}, {'k', kConsonant}, {'l', kConsonant}, {'m', kConsonant}, {'n', kConsonant}, {'o', kVowel}, {'p', kConsonant}, {'q', kConsonant}, {'r', kConsonant}, {'s', kConsonant}, {'t', kConsonant}, {'u', kVowel}, {'w', kConsonant}, {'v', kConsonant}, {'x', kConsonant}, {'q', kConsonant}, {'y', kVowel}, {'z', kConsonant}, }; // for (char a = 'a'; a <= 'z';a++) // if (characters.find(a) == characters.end()) // cout<<a<<" is absent" << endl; string str; cin >> str; int n = str.size(); vector<bool> V(n); vector<bool> lastThree; vector<int> occurrences; for (int i = 0;i < str.size();i++) { char a = str[i]; V[i] = characters[a] == kConsonant; lastThree.push_back(V[i]); if (isThreeInRow(lastThree)) { occurrences.push_back(i - 2); } } ///in occurrences the beginnings of 3 same types long long int sum = 0; long long int prev = -1; for (int i = 0;i < occurrences.size();i++) { int x = occurrences[i]; sum += (long long int)(x - prev) * (long long int)(n - (x + 2)); prev = x; } cout << sum << endl; 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 | #include <iostream> #include <set> #include <vector> #include <map> #include <unordered_map> using namespace std; constexpr int kConsonant = 1; constexpr int kVowel = -1; bool isThreeInRow(vector<bool> &V) { if (V.size() < 3) return false; for (int i = V.size() - 3;i < V.size() - 1;i++) if (V[i] != V[i + 1]) return false; return true; } int main() { ios_base::sync_with_stdio(0); map<char, int> characters = { {'a', kVowel}, {'b', kConsonant}, {'c', kConsonant}, {'d', kConsonant}, {'e', kVowel}, {'f', kConsonant}, {'g', kConsonant}, {'h', kConsonant}, {'i', kVowel}, {'j', kConsonant}, {'k', kConsonant}, {'l', kConsonant}, {'m', kConsonant}, {'n', kConsonant}, {'o', kVowel}, {'p', kConsonant}, {'q', kConsonant}, {'r', kConsonant}, {'s', kConsonant}, {'t', kConsonant}, {'u', kVowel}, {'w', kConsonant}, {'v', kConsonant}, {'x', kConsonant}, {'q', kConsonant}, {'y', kVowel}, {'z', kConsonant}, }; // for (char a = 'a'; a <= 'z';a++) // if (characters.find(a) == characters.end()) // cout<<a<<" is absent" << endl; string str; cin >> str; int n = str.size(); vector<bool> V(n); vector<bool> lastThree; vector<int> occurrences; for (int i = 0;i < str.size();i++) { char a = str[i]; V[i] = characters[a] == kConsonant; lastThree.push_back(V[i]); if (isThreeInRow(lastThree)) { occurrences.push_back(i - 2); } } ///in occurrences the beginnings of 3 same types long long int sum = 0; long long int prev = -1; for (int i = 0;i < occurrences.size();i++) { int x = occurrences[i]; sum += (long long int)(x - prev) * (long long int)(n - (x + 2)); prev = x; } cout << sum << endl; return 0; } |