思维很重要🤔
这种题就不能单单想暴力了
首先这个题正确的思维是:假设0位置和n+1是‘R’,找除了0到n+1以外最远的R像个距离,该距离就是d。
这一题的关键是理解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)].中的 [max(0,i−d);i−1]和[i+1;min(n+1;i+d)]。
当i位置是L时,跳的范围时i-d(当i-d<0时取0)到i-1,当i的位置时R时跳的范围是i+1到i+d(当i+d>n+1时取n+1),当时我就吧跳的范围长度当作随i的变化时刻时刻变化的啦,没有了解到这两个式子的真正含义。┭┮﹏┭┮,还要努力。
这样的话,这题的思维就可以理解了。

Example
input
6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
output
3
2
3
1
7
1
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int m=-1,step=0;
int len=s.length();
for(int i=0;i<len;i++)
{
if(s[i]=='L')
step++;
else if(s[i]=='R')
{
m=max(m,step);
step=0;
}
}
m=max(m,step);
if(m==-1)
m=len;
cout<<m+1<<endl;
}
}
本文深入解析了一道跳跃游戏的算法题目,强调了正确思维的重要性。通过将问题转化为寻找最长跳跃距离,文章详细解释了如何理解跳跃范围的数学表达,并分享了作者从初识困惑到最终理解的心路历程。

320

被折叠的 条评论
为什么被折叠?



