1324C - Frog Jumps[思维]
time limit per test | memory limit per test | input | output |
---|---|---|---|
2 seconds | 256 megabytes | standard input | standard output |
Description:
There is a frog staying to the left of the string s = s 1 s 2 … s n s=s_1s_2…s_n s=s1s2…sn consisting of n n n characters (to be more precise, the frog initially stays at the cell 0 0 0). Each character of s is either ′ L ′ 'L' ′L′ or ′ R ′ 'R' ′R′. It means that if the frog is staying at the i i i-th cell and the i i i-th character is ′ L ′ 'L' ′L′, the frog can jump only to the left. If the frog is staying at the i i i-th cell and the i i i-th character is ′ R ′ 'R' ′R′, the frog can jump only to the right. The frog can jump only to the right from the cell 0 0 0.
Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.
The frog wants to reach the n + 1 n+1 n+1-th cell. The frog chooses some positive integer value d d d before the first jump (and cannot change it later) and jumps by no more than d d d cells at once. I.e. if the i i i-th character is ′ L ′ 'L' ′L′ then the frog can jump to any cell in a range [ m a x ( 0 , i − d ) , i − 1 ] [max(0,i−d), i−1] [max(0,i−d),i−1], and if the i i i-th character is ′ R ′ 'R' ′R′ then the frog can jump to any cell in a range [ i + 1 , m i n ( n + 1 , i + d ) ] [i+1, min(n+1, i+d)] [i+1,min(n+1,i+d)].
The frog doesn’t want to jump far, so your task is to find the minimum possible value of d d d such that the frog can reach the cell n + 1 n+1 n+1 from the cell 0 0 0 if it can jump by no more than d d d cells at once. It is guaranteed that it is always possible to reach n + 1 n+1 n+1 from 0 0 0.
You have to answer t t t independent test cases.
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) — the number of test cases.
The next t t t lines describe test cases. The i i i-th test case is described as a string s s s consisting of at least 1 1 1 and at most 2 ⋅ 1 0 5 2⋅10^5 2⋅105 characters ′ L ′ 'L' ′L′ and ′ R ′ 'R' ′R′.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2 ⋅ 1 0 5 ( ∑ ∣ s ∣ ≤ 2 ⋅ 1 0 5 ) 2⋅10^5 (\sum |s|≤2⋅10^5) 2⋅105(∑∣s∣≤2⋅105)…
Output
For each test case, print the answer — the minimum possible value of d such that the frog can reach the cell n + 1 n+1 n+1 from the cell 0 0 0 if it jumps by no more than d d d at once.
Example input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
Example output
3
2
3
1
7
1
Note
The picture describing the first test case of the example and one of the possible answers:
In the second test case of the example, the frog can only jump directly from 0 0 0 to n + 1 n+1 n+1.
In the third test case of the example, the frog can choose d = 3 d=3 d=3, jump to the cell 3 3 3 from the cell 0 0 0 and then to the cell 4 4 4 from the cell 3 3 3.
In the fourth test case of the example, the frog can choose d = 1 d=1 d=1 and jump 5 5 5 times to the right.
In the fifth test case of the example, the frog can only jump directly from 0 to n + 1 n+1 n+1.
In the sixth test case of the example, the frog can choose d = 1 d=1 d=1 and jump 2 2 2 times to the right.
分析:
题意:
给一个长度为
n
n
n的字符串,位置分别在
[
1
,
n
]
[1,n]
[1,n]中
青蛙从
0
0
0开始跳,最大距离可以跳
d
d
d
在每个位置可以跳的方向为所在位置字符串的方向
′
L
′
'L'
′L′就只能往左跳,
′
R
′
'R'
′R′就只能往右跳
最终要达到
n
+
1
n+1
n+1的位置,问最小的
d
d
d应该是多少
做法:
只要计算相邻的
′
R
′
'R'
′R′之间的最大距离,注意
n
+
1
n+1
n+1位置也要当成一个
′
R
′
'R'
′R′,因为最后一定要跳到这个位置
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int read() {
int x = 0, f = 0; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-')f = -f; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x<<3)+(x<<1)+ch-'0'; ch = getchar();}
return x;
}
char str[maxn];
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%s", str + 1);
int len = strlen(str + 1) + 1, pre = 0, ans = 0;
str[len] = 'R';
for(int i = 1; i <= len; ++i) {
if(str[i] == 'L') continue;
ans = max(ans, i - pre);
pre = i;
}
printf("%d\n", ans);
}
return 0;
}