fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7. int L,O;
  8. string s;
  9. cin >> s;
  10. for(int i=0;i<n;i++){
  11. if(s[i] == 'L')
  12. L++;
  13. else
  14. O++;
  15. }
  16. if(L % 2 != 0 && O % 2 != 0)
  17. cout << n/2 << endl;
  18. else{
  19. int l = 1;
  20.  
  21. for(int j=1;j<s.size();j++){
  22. int L_left = 0, L_right = 0, O_left = 0, O_right = 0;
  23. int l = j;
  24. for(int i=0;i<l;i++){
  25. if(s[i] == 'L')
  26. L_left++;
  27. else
  28. O_left++;
  29. }
  30. for(int i=l;i<s.size();i++){
  31. if(s[i] == 'L')
  32. L_right++;
  33. else
  34. O_right++;
  35. }
  36. //cout << L_left <<" "<< L_right << " " << O_left << " " << O_right << endl;
  37. if(L_left != L_right && O_left != O_right){
  38. cout << l << endl;
  39. return 0;
  40. }
  41. l++;
  42. }
  43. cout << "-1";
  44. }
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5272KB
stdin
10
LLOOOOLLLO






stdout
5