码迷,mamicode.com
首页 > 编程语言 > 详细

给大佬的奖杯排序(CodeForces - 1082B)

时间:2019-01-20 15:04:51      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:for   return   input   技术分享   com   mos   beautiful   nsis   get   

题目链接:http://codeforces.com/problemset/problem/1082/B

B. Vova and Trophies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova 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.

Input

The first line contains one integer nn (2n1052≤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.

Output

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

Examples

 

input
Copy
10
GGGSGGGSGG
output
Copy
7
input
Copy
4
GGGG
output
Copy
4
input
Copy
3
SSS
output
Copy
0

 

Note

In 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.

大佬的奖杯真是多,而且大佬想要把尽量多的金杯放在一起(手动滑稽).不过他只想最多交换一次。这道题的要求就是让你写个代码把尽量多的的奖杯放在一起

但是你只能交换一次或者不交换。

题目的大体思维:用两个数保存两个连续区段奖杯的数量。不过只有当两个连续区段相差为1的时候才能够把两个区段加起来+1,否则只能自加1.

但是这道题有特殊情况,比如当奖杯排序为GGGGSGGGG的时候,这个时候并不能去交换再去+1,因此只能用另一个数来保存连续奖杯的数量

此后再取一下ans和统计gold奖杯的小值就可以了

附上丑陋的AC代码

技术分享图片
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 string a;
 5 int main()
 6 {
 7     int n,ans=0,l=0,sumg=0,r=0;
 8     cin>>n;
 9     cin>>a;
10     for(int i=0;i<n;i++)
11     {
12         if(a[i]==G)
13         {
14             sumg++;
15            r++;
16         }
17         else
18         {
19             l=r;
20            r=0;
21         }
22         ans=max(ans,r+l+1);
23     }
24     ans=min(ans,sumg);
25     printf("%d",ans);
26     return 0;
27 }
View Code

人生第二篇博客。。。。。。。(参考其他人的,因为比赛的时候没有AC这道题)

 

 

给大佬的奖杯排序(CodeForces - 1082B)

标签:for   return   input   技术分享   com   mos   beautiful   nsis   get   

原文地址:https://www.cnblogs.com/tombraider-shadow/p/10294730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!