[Codeforces Round #627]1324C - Frog Jumps[思维]

本文探讨了一种典型的算法问题——青蛙跳跃,青蛙需从初始位置0跳至目标位置n+1,每次跳跃方向由字符串中的字符决定。文章提供了一种有效的解决方案,即计算相邻′R′之间的最大距离作为最小跳跃距离。

1324C - Frog Jumps[思维]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard 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=s1s2sn 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,id),i1], 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(1t104) — 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 2105 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) 2105(s2105)

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;
}




内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值