题目链接:http://codeforces.com/problemset/problem/1082/B
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.
The 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.
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
Examples
10
GGGSGGGSGG
7
4
GGGG
4
3
SSS
0
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 }
人生第二篇博客。。。。。。。(参考其他人的,因为比赛的时候没有AC这道题)