fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. string step;
  8. cin>>step;
  9. int stepMax=0;
  10. int L=0, R=0;
  11. int Lm=0, Rm=0;
  12. for (int i=0; i<step.length(); i++)
  13. {
  14. if (step[i]=='L')
  15. {
  16. L++;
  17. Lm++;
  18. if (Lm-R>stepMax) stepMax=Lm-R;
  19. }
  20. else if (step[i]=='R')
  21. {
  22. R++;
  23. Rm++;
  24. if (Rm-L>stepMax) stepMax=Rm-L;
  25. }
  26. else
  27. {
  28. Lm++;
  29. if (Lm-R>stepMax) stepMax=Lm-R;
  30. Rm++;
  31. if (Rm-L>stepMax) stepMax=Rm-L;
  32. }
  33. }
  34. cout<<stepMax;
  35. return 0;
  36. }
Success #stdin #stdout 0s 15240KB
stdin
R???L
stdout
4