fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23. // In Good Subarrays problem, draw array && subarray and apply Maths and find condition
  24.  
  25. // L R
  26. // [ _ _ _ _ _ (_ _ _ _ _ _) _ _ ]
  27.  
  28.  
  29. //* Count of good subarrays
  30. //* with equal x,y,z,w,b
  31.  
  32. //* Eg : [x y p z a b p x y z a b]
  33. //* [(x y p z a b) p x y z a b]
  34. //* [x (y p z a b p x) y z a b]
  35. //* [x y p (z a b p x y) z a b]
  36. //* [x y p z (a b p x y z) a b]
  37. //* [x y p z a (b p x y z a) b]
  38. //* [x y p z a b p (x y z a b)]
  39.  
  40. vector<string> a;
  41. void consistency(int n) {
  42.  
  43. int x = 0;
  44. int y = 0;
  45. int z = 0;
  46. int w = 0;
  47. int b = 0;
  48.  
  49. //* {x,y,z,w,b} -> freq
  50. map<vector<int>, int> mp = {{{0,0,0,0}, 1}};
  51. int ans = 0;
  52. for(int i=0; i<n; i++){
  53. if(a[i] == "x") x++;
  54. if(a[i] == "y") y++;
  55. if(a[i] == "z") z++;
  56. if(a[i] == "w") w++;
  57. if(a[i] == "b") b++;
  58.  
  59. vector<int> diff = {x-y, y-z, z-w, w-b};
  60. if(mp.count(diff)){
  61. ans += mp[diff];
  62. }
  63. mp[diff]++;
  64.  
  65. }
  66.  
  67. cout << ans << endl;
  68. }
  69.  
  70. void solve() {
  71.  
  72. int n;
  73. cin >> n;
  74. a.resize(n);
  75. for(int i=0; i<n; i++) cin >> a[i];
  76. consistency(n) ;
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int32_t main() {
  85. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  86.  
  87. int t = 1;
  88. // cin >> t;
  89. while (t--) {
  90. solve();
  91. }
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5300KB
stdin
12
x y p z w b p x y z w b
stdout
12