传送门:CF—1324C
测试样例:
input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
output
3
2
3
1
7
1
题目大意:
t个测试样例,每个样例给定一个字符串,只包含"L"和"R"的指令。表示可以向左或向右跳动(第一个位置只能向右跳动,可以跳到同一个位置两次)。现在有一只青蛙在0号位置,要跳到n+1号位置,在跳跃之前选定一个距离d且不能改变,每次的跳跃距离不能超过d。问可选d的最小值是多少。
题目分析:
一开始被Note中的第一个图示迷惑了,其实我们可以不考虑向左跳的情况。只需要找到0到第一个R,相邻两个R,和最后一个R到n+1的距离最大值即可。我们找到的最大距离d是保证我们能从0跳到n+1位置的符合题目要求的最小的d。
AC代码:
#include<bits/stdc++.h>
using namespace std;
char s[200005];
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%s",s+1);
int len=strlen(s+1);
int x=0,ans=0;
for(int i=1;i<=len;i++)
if(s[i]=='R')
{
ans=max(ans,i-x);
x=i;
}
ans=max(ans,len+1-x);
cout<<ans<<endl;
}
return 0;
}