Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Model the string as a deterministic path: each student has exactly one outgoing edge, either to the left neighbor or the right neighbor.
Starting from student , the ball moves right while it keeps seeing R. So the beginning of the string matters a lot more than the rest.
Let be the first position where using -based indexing. Before , every character is R, so the ball visits in order.
Once the ball reaches student , it goes to . But , so student sends it right back to . The process is now trapped forever between those two students.
The answer is exactly the first L position . Since the first L is preceded by R, this is also s.find("RL") + 2 in zero-based C++ indexing.
Research notes: I checked the official Codeforces editorial, which points to the compact RL-position solution, and CF Step, which shows the direct simulation idea. The supplied statement is still the source of truth; these just confirm the intended direction.
Let positions be -indexed. Define
This always exists because the last character is L, and because the first character is R.
For every position , we have by definition of the first L. So the ball starts at student , then goes
That already visits exactly the students .
Now look at what happens at student . Since , student passes the ball to . But was the first L, so , meaning student passes the ball right back to .
So after reaching , the ball gets stuck in the 2-cycle
No later student can ever be reached. They might have all kinds of spicy letters over there, but the ball does not care. It is trapped.
Because , the process has enough passes to reach student before it ends. Therefore the set of students who receive the ball is exactly
so the answer is .
In C++ zero-based indexing, the first L is at index . Equivalently, the first substring RL starts at index , so
Either s.find('L') + 1 or s.find("RL") + 2 works. We use the latter since it matches the intended observation cleanly.
Edge cases:
L are irrelevant because the ball never reaches them.Complexity per test case is time and extra memory. With , this is tiny. Like, not even worth waking the CPU up for.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
cout << int(s.find("RL")) + 2 << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
cout << int(s.find("RL")) + 2 << '\n';
}
}