fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. string s="xyxxy";
  7. int n=s.size();
  8. int d[n];
  9. for(int i=0;i<n;i++){
  10. if(s[i]=='x'){
  11. d[i]=-1;
  12. }
  13. else if(s[i]=='y'){
  14. d[i]=1;
  15. }
  16. }
  17.  
  18. unordered_map<int,int>count;
  19. count[0]=1;
  20. int f_count=0;
  21. int sum=0;
  22. for(int i=0;i<n;i++){
  23. sum+=d[i];
  24. if(count.find(sum)!=count.end()){
  25. f_count++;
  26. }
  27. count[sum]++;
  28. }
  29. cout<<f_count;
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
3