fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <numeric>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <stack>
  10. #include <queue>
  11. using namespace std;
  12. #define all(a) a.begin(), a.end()
  13. #define ll long long
  14. #define fo(i,n) for (long long i = 0; i < n; i++)
  15. int main()
  16. {
  17. ios::sync_with_stdio(false);
  18. cin.tie(nullptr);
  19.  
  20. int t;
  21. cin >> t;
  22. while(t--)
  23. {
  24. ll n, x, k;
  25. cin >> n >> x >> k;
  26. string s;
  27. cin >> s;
  28.  
  29. ll current = x;
  30. ll t = 0;
  31. bool reachedZero = false;
  32. for (int i = 0; i < n; i++)
  33. {
  34. t++;
  35. if (s[i]=='L')
  36. {
  37. current--;
  38. }
  39. else
  40. {
  41. current++;
  42. }
  43. if (current == 0)
  44. {
  45. reachedZero = true;
  46. break;
  47. }
  48. }
  49.  
  50. if(!reachedZero)
  51. {
  52. cout << 0 << "\n";
  53. continue;
  54. }
  55.  
  56. ll count = 1;
  57. ll remaining = k - t;
  58. if (remaining < 0)
  59. {
  60. cout << count << "\n";
  61. continue;
  62. }
  63.  
  64.  
  65. current = 0;
  66. ll cycle_time = 0;
  67. bool cycleReset = false;
  68. for (int i = 0; i < n; i++)
  69. {
  70. cycle_time++;
  71. if (s[i]=='L')
  72. {
  73. current--;
  74. }
  75. else
  76. {
  77. current++;
  78. }
  79. if (current == 0)
  80. {
  81. cycleReset = true;
  82. break;
  83. }
  84. }
  85. if (!cycleReset)
  86. {
  87. cout << count << "\n";
  88. }
  89. else
  90. {
  91. cout << count + remaining / cycle_time << "\n";
  92. }
  93. }
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0.01s 5288KB
stdin
6
3 2 6
LLR
2 -1 8
RL
4 -2 5
LRRR
5 3 7
LRRLL
1 5 2
LRLLLLL
3 -1 4846549234412827
RLR
stdout
1
4
1
0
0
2423274617206414