fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. string s;
  7. cin>>s;
  8. set <pair<int,int>> st;
  9.  
  10. int n=s.size(),t=1e9;
  11. int x=0,y=0,ans=0;
  12. char dir=0;
  13.  
  14.  
  15. for (int i=0;i<n;i++){
  16. st.insert({x,y});
  17. if (st.find({x,y})!=st.end()&&ans!=0){
  18. cout<<ans;
  19. return 0;
  20. }
  21. if (s[i]=='S'){
  22. ans++;
  23. if (dir==0){
  24. y++;
  25. }
  26. if (dir==1){
  27. x++;
  28. }
  29. if (dir==2){
  30. y--;
  31. }
  32. if (dir==3){
  33. x--;
  34. }
  35. }
  36. if (s[i]=='L'){
  37. dir=(dir==0? 3:(dir-1));
  38. }
  39. if (s[i]=='R'){
  40. dir++;
  41. dir%=4;
  42. }
  43. }
  44. cout<<-1;
  45. return 0;
  46. }
Success #stdin #stdout 0s 4480KB
stdin
RSLLSSRRSRSRRSS
stdout
1