传送门
题意
青蛙想从0这个位置跳到n+1这个位置。给你一个从1到n的字符串,字符串只包括L和R,L只能向左走,R只能向右走,青蛙将选定一个最大跳数d,使得遇到L后往左走[1,d]不能低于0,遇到R后往右走[1,d]不能超过n+1.求最小的最大跳数。
思路
不管青蛙向左走的情况,只管向右走的情况(因为如果向左走青蛙总会回到这个点)。青蛙必须越过最长连续的L,不然只能来回跳跃达不到终点。只需求最长的L即可。
AC代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=2e5+5;
char str[MAXN];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",&str);
int len=strlen(str);
int mx=1,cnt=1;
for(int i=0;i<len;i++)
{
if(str[i]=='L') cnt++;
else
{
mx=max(mx,cnt);
cnt=1;
}
}
mx=max(mx,cnt);
printf("%d\n",mx);
}
return 0;
}