Frog Jumps

There is a frog staying to the left of the string s=s1s2…sn consisting of n characters (to be more precise, the frog initially stays at the cell 0). Each character of s is either ‘L’ or ‘R’. It means that if the frog is staying at the i-th cell and the i-th character is ‘L’, the frog can jump only to the left. If the frog is staying at the i-th cell and the i-th character is ‘R’, the frog can jump only to the right. The frog can jump only to the right from the cell 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-th cell. The frog chooses some positive integer value d before the first jump (and cannot change it later) and jumps by no more than d cells at once. I.e. if the i-th character is ‘L’ then the frog can jump to any cell in a range [max(0,i−d);i−1], and if the i-th character is ‘R’ then the frog can jump to any cell in a range [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 such that the frog can reach the cell n+1 from the cell 0 if it can jump by no more than d cells at once. It is guaranteed that it is always possible to reach n+1 from 0.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.

The next t lines describe test cases. The i-th test case is described as a string s consisting of at least 1 and at most 2⋅105 characters ‘L’ and ‘R’.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed 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 from the cell 0 if it jumps by no more than d at once.

Example
inputCopy
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
outputCopy
3
2
3
1
7
1
题意:
青蛙在0位置要到n+1的位置,0到n+1之间有长为n的,只包含L或R的字符串,L说明可以往左跳,R说明可以向右跳,每次最多跳d的长度

问你保证能跳到n+1的位置上的情况下,d的最小值

既然青蛙要从最左边跳到最右边,前提是它要往右跳
我们知道,只有字符串中出现‘R’时,青蛙才会往右跳
如果d>n,那么任何情况下,一次就能跳到n+1
题目要求的是d的最小值,那么我们尽量保证d最小
0是起跳位置,n+1的位置是要跳到的,方便起见可以当它们为R
既然我们要保证能跳到,那么d肯定要大于等于相邻R之间的距离,所以相邻R之间距离的最大值就是所求解

在这里插入图片描述
以“LRLRRLL”为例(见图片)
样例输出为3
把0和n+1的位置也标记为R
五个R的下标分别为0,2,4,5,8
相邻R之间的距离分别为2,2,1,3
那么3就是相邻R之间距离的最大值
也就是答案的解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<vector>
constexpr auto INF = 0x3f3f3f3f;
const double PI = 3.14;
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--)
	{
		string s;
		cin >> s;
		int now = -1;
		int ans = 0;
		int n = s.size();
		for (int i = 0; i < n; i++)
		{
			if (s[i] == 'R')
			{
				ans = max(i - now, ans);
				now = i;
			}
		}
		ans = max(ans, n-now);
		cout << ans << endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值