C. Frog Jumps---------------------思维

在这里插入图片描述
在这里插入图片描述

题意:
给定一个字符串只包含"L" “R”
L:向左跳0~d步
R:向右跳0~d步
询问d取多少时,可以跳到n+1,d要最小。起点是0

解析:
突破口:我要跳到n+1,肯定都要跳"R" 所以我们只要计算相邻"R"之间的距离再加上起点到第一个"R"的距离和终点到最后一个"R"的距离

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1000;
map<char,int> v;
char s[N];
int t;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>(s+1);
		int m=strlen(s+1);
		int mx=-1;
		v['R'-'A']=0;
		for(int i=1;i<=m;i++)
		{
			if(s[i]=='R')
			{
				mx=max(mx,i-v[s[i]-'A']);
				v[s[i]-'A']=i;
			}
		}
		mx=max(mx,m+1-v['R'-'A']);
		if(mx==-1) cout<<m+1<<endl;
		else cout<<mx<<endl;
	}
}
发布了491 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104858291