fork download
  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<iomanip>
  5. #include<climits>
  6. #include<numeric>
  7. #include<set>
  8. #include<sstream>
  9. #include<map>
  10. #include<vector>
  11. #include<string>
  12. #include<regex>
  13.  
  14. using namespace std;
  15.  
  16. const long long MOD = (long long)(1e9 + 7);
  17.  
  18. vector<string> split(const string& haystack, const string& needle) {
  19. vector<string> result;
  20. size_t start_pos = 0;
  21. size_t found_pos = haystack.find(needle, start_pos);
  22. while (found_pos != string::npos) {
  23. size_t count = found_pos - start_pos;
  24. string token = haystack.substr(start_pos, count);
  25. if (!token.empty()) {
  26. result.push_back(token);
  27. }
  28. start_pos = found_pos + needle.length();
  29. found_pos = haystack.find(needle, start_pos);
  30. }
  31.  
  32. string last_token = haystack.substr(start_pos);
  33. if (!last_token.empty()) {
  34. result.push_back(last_token);
  35. }
  36. return result;
  37. }
  38. string make_key(int x, int y, int z) {
  39. stringstream builder;
  40. builder << x << "-" << y << "-" << z;
  41. return builder.str();
  42. }
  43. int main() {
  44. string s;cin >> s;
  45. map<string, int> prefix;
  46. int d2 = 0, d8 = 0, dt = 0;
  47. int sum = 0;
  48. prefix[make_key(0, 0, 0)] = 1;
  49. for (int i = 0;i < s.length();i++) {
  50. if (s[i] == '2')d2++;
  51. if (s[i] == '8')d8++;
  52. if (s[i] == 't')dt++;
  53.  
  54. int x = d2 - d8, y = d2 - dt, z = d8 - dt;
  55. string key = make_key(x, y, z);
  56. sum = sum + prefix[key];
  57. prefix[key]++;
  58. }
  59. cout << sum;
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty