codeforces 1324 C. Frog Jumps(贪心/二分)
There is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00 ). Each character of ss is either 'L' or 'R'. It means that if the frog is staying at the ii -th cell and the ii -th character is 'L', the frog can jump only to the left. If the frog is staying at the ii -th cell and the ii -th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00 .
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+1n+1 -th cell. The frog chooses some positive integer value dd before the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii -th character is 'L' then the frog can jump to any cell in a range [max(0,i−d);i−1][max(0,i−d);i−1] , and if the ii -th character is 'R' then the frog can jump to any cell in a range [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 dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00 .
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104 ) — the number of test cases.
The next tt lines describe test cases. The ii -th test case is described as a string ss consisting of at least 11 and at most 2⋅1052⋅105 characters 'L' and 'R'.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2⋅1052⋅105 (∑|s|≤2⋅105∑|s|≤2⋅105 ).
Output
For each test case, print the answer — the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.
Example
6 LRLRRLL L LLR RRRR LLLLLL R
3 2 3 1 7 1
这个题队友用二分写的== 我当时想的贪心直接扫一遍就行...
懒得写了,放上队友的二分代码吧。
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * 1e5 + 10; char s[maxn]; int n; bool check(int d) { int p = 0; while(p + d < n+1) { bool ok = false; for(int i = p + d; i >= p + 1; i--) if(s[i] == 'R') { p = i; ok = true; break; } if(ok == false) break; } return p + d >= n + 1; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while(t--) { memset(s, 0, sizeof s); cin >> s + 1; n = strlen(s + 1); int l = 0, r = n + 1; while(r - l > 1) { int mid = (l + r) / 2; if(check(mid)) r = mid; else l = mid; } printf("%d\n", r); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!