fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long int
  5.  
  6. int main() {
  7. string s;
  8. getline(cin,s);
  9. // string of 'x' and 'y'
  10. // possible operations => (xy - yz) and (yx - zy)
  11.  
  12. // first count occurences of x
  13. ll count=0;
  14. for(char c:s){
  15. if(c=='x')count++;
  16. }
  17.  
  18. // now check if 'yy' present or not
  19. if(s.find("yy")!=string::npos){
  20. cout << count;
  21. return 0;
  22. }
  23. else{
  24. // we need to subtract length of smallest x block from count value
  25. int blockSize=0;
  26. int minSize=1e9;
  27. for(int i=0;i<s.length();i++){
  28. if(s[i]=='x')blockSize++;
  29. else{
  30. minSize = min(minSize,blockSize);
  31. blockSize=0;
  32. }
  33. }
  34. minSize = min(minSize,blockSize);
  35. cout << count-minSize;
  36. }
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
xyyxyxxxyyxyyxxyxyyxxxyxxxyyyyxxx
stdout
18