codeforces 1324 C. Frog Jumps(简单)

C. Frog Jumps(简单)

题目链接:codeforces 1324C

题意:

   给出一个字符串, 只包含两个字符‘L’,‘R’,如果为L,只能向左走,如果为R,只能向右走,问一只青蛙从最左端开始走,走到最右端一次最少跳多远。这只青蛙可以跳的距离为 [ 1,最短长度 ] 这个区间。

举例:LRLRRLL,最短跳的距离为 3

首先从 0 位置跳到 第一个 'R', 然后从第一个‘R’跳到第二个或者第三个 'R',最终跳到末尾。

在这个样例中,最短跳的距离为 3, 意味着这个青蛙可以向右跳1步,或者2步,或者3步。

解题思路:

    可以将字符串两端加入R,判断两个R的最远距离即可,但注意 最短距离下限为 1 而不是 0。

#include <iostream> 
using namespace std;
typedef long long ll;
const int mod = 1e9 +7;
int main() {
	int t;
   	cin >> t;
   	while (t--) {
   		string s;
   		cin >> s;
   		int len = s.size();
   		s[len] = 'R';
   		int ans = 1;
   		int j = len;
   		for(int i = len - 1; i >= 0; i--){
   			if(s[i] == 'R'){
   				ans = max(ans, j - i);
   				j = i;
   			}
   		}
   		if(s[0] != 'R'){
   			ans = max(ans, j + 1);
   		}
   		cout << ans << endl;
	}
    return 0;
}
发布了204 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/error311/article/details/104837950