#include <bits/stdc++.h> using namespace std; # define L long long string s; int n; L oneLetterWords() { char currentLetter = 'd'; int currentLength = 0; L result = 0; for (int i = 0; i < n; i++) { if (s[i] == currentLetter) { currentLength++; result += (L)currentLength; } else { currentLength = 1; currentLetter = s[i]; result++; } } return result; } L twoLetterWords(char ch1, char ch2) { vector<L> count(2 * n + 1); int diff = 0; L result = 0; int d = 0; for (int i = 0; i < n; i++) { if (s[i] == ch1) { diff++; count[n - diff + 1]++; result += count[n - diff]; d++; } else if (s[i] == ch2) { diff--; count[n - diff - 1]++; result += count[n - diff]; d++; } else { for (int j = 0; j <= d; j++) { count[n - j] = count[n + j] = 0; } diff = 0; d = 0; } } return result; } L threeLetterWords() { map<pair<int, int>, L> count; int diffAB = 0, diffAC = 0; L result = 0; for (int i = 0; i < n; i++) { if (s[i] == 'a') { diffAB++; diffAC++; count[make_pair(-diffAB + 1, -diffAC + 1)]++; } else if (s[i] == 'b') { diffAB--; count[make_pair(-diffAB - 1, -diffAC)]++; } else { diffAC--; count[make_pair(-diffAB, -diffAC - 1)]++; } result += count[make_pair(-diffAB, -diffAC)]; } return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = int(s.length()); cout << oneLetterWords() + twoLetterWords('a', 'b') + twoLetterWords('a', 'c') + twoLetterWords('b', 'c') + threeLetterWords(); }
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 | #include <bits/stdc++.h> using namespace std; # define L long long string s; int n; L oneLetterWords() { char currentLetter = 'd'; int currentLength = 0; L result = 0; for (int i = 0; i < n; i++) { if (s[i] == currentLetter) { currentLength++; result += (L)currentLength; } else { currentLength = 1; currentLetter = s[i]; result++; } } return result; } L twoLetterWords(char ch1, char ch2) { vector<L> count(2 * n + 1); int diff = 0; L result = 0; int d = 0; for (int i = 0; i < n; i++) { if (s[i] == ch1) { diff++; count[n - diff + 1]++; result += count[n - diff]; d++; } else if (s[i] == ch2) { diff--; count[n - diff - 1]++; result += count[n - diff]; d++; } else { for (int j = 0; j <= d; j++) { count[n - j] = count[n + j] = 0; } diff = 0; d = 0; } } return result; } L threeLetterWords() { map<pair<int, int>, L> count; int diffAB = 0, diffAC = 0; L result = 0; for (int i = 0; i < n; i++) { if (s[i] == 'a') { diffAB++; diffAC++; count[make_pair(-diffAB + 1, -diffAC + 1)]++; } else if (s[i] == 'b') { diffAB--; count[make_pair(-diffAB - 1, -diffAC)]++; } else { diffAC--; count[make_pair(-diffAB, -diffAC - 1)]++; } result += count[make_pair(-diffAB, -diffAC)]; } return result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; n = int(s.length()); cout << oneLetterWords() + twoLetterWords('a', 'b') + twoLetterWords('a', 'c') + twoLetterWords('b', 'c') + threeLetterWords(); } |