B. Vova and Trophies


#include<bits/stdc++.h>

using namespace std;

int main()

{

   
int num;

   
cin>>num;char c;

   
int g=0,g1=0,pre=0;int ans=0;

   
while(num--)

    {

       
cin>>c;

       
if(c=='G') g++,g1++;

       
else

       
{

           
pre=g1;

           
g1=0;

       
}

       
ans=max(ans,pre+g1+1);

 

 

    }

   
ans=min(g,ans);

   
cout<<ans<<endl;

   
return 0;

}

B. Vova and Trophiestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.InputThe first line contains one integer nn (2≤n≤1052≤n≤105) — the number of trophies.The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the ii-th trophy is a golden one, otherwise it’s a silver trophy.OutputPrint the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.ExamplesinputCopy10
GGGSGGGSGG
outputCopy7
inputCopy4
GGGG
outputCopy4
inputCopy3
SSS
outputCopy0
NoteIn the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence “GGGGGGGSGS”, the length of the longest subsegment of golden trophies is 77.In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44.In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 00.

此题动态规划,一般来说题目不同的,是根据特征来做的,开始难以下手,由于是隔一个是可以连接在一起的,隔两个重新计算,要保存每一组G的数目,间隔一个就要更新,同时要保存前面一组G的数目,确定变量,用循环。由于可能在操作后,G变成了一组,但是G的个数不一定够用,用上述的状态转移方程,变大了,所以与G的总数量比较。
总而言之,相当于一道普通数学题的难度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值