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 (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.
Output
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
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.
当时做题的时候交了一发过了,不过最后竟然被hack了,哎,还是太弱了,要自闭了。嘤嘤嘤;
题意很简单,就不说了,
思路:
1、我的思路:找一下连续的G串有多少串sumg并记录最大值maxx1,然后再找 如果一个S的左边和右边都是G串那么再记录一下此类串的最大长度maxx2
如果sumg==1输出maxx1
如果sumg==2输出max(maxx1+1,maxx2);
如果sumg>2输出max(maxx1+1,maxx2+1);
2、大佬的思路:
记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 'G' 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen
以下是鄙人的代码,很繁琐;
下面还有大佬的代码;
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <map> 9 using namespace std; 10 typedef long long ll; 11 const int maxn=1e5+5; 12 const int INF=0x3f3f3f3f; 13 char a[maxn]; 14 int main() 15 { 16 int n,sumg=0, tempg=0; 17 scanf("%d",&n); 18 getchar(); 19 scanf("%s",a); 20 int maxx1=0,maxx2=0,temps=0,flag=0,CNT=0; 21 for(int i=0;i<n;i++) 22 { 23 if(a[i]=='G') 24 { 25 sumg++; 26 tempg++; 27 if(i==n-1) 28 { 29 if(temps) 30 maxx1=max(maxx1,temps+tempg); 31 else 32 maxx2=max(maxx2,tempg); 33 CNT++; 34 } 35 } 36 else 37 { 38 if(tempg)CNT++; 39 if(flag==1) 40 { 41 maxx1=max(tempg+temps,maxx1); 42 temps=tempg; 43 tempg=0; 44 flag=1; 45 } 46 if(a[i+1]!='G') 47 { 48 49 maxx2=max(maxx2,tempg); 50 tempg=0; 51 temps=0; 52 } 53 else 54 { 55 if(tempg&&a[i-1]=='G') 56 flag=1; 57 temps+=tempg; 58 tempg=0; 59 } 60 } 61 } 62 if(sumg==n) 63 printf("%d ",n); 64 else if(sumg) 65 { 66 if(CNT==2) 67 { 68 printf("%d ",max(maxx1,maxx2+1)); 69 } 70 else if(CNT==1) 71 { 72 printf("%d ",maxx2); 73 } 74 else 75 { 76 printf("%d ",max(maxx1+1,maxx2+1)); 77 } 78 } 79 else 80 printf("0 "); 81 return 0; 82 }
一下是大佬的代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <map> 9 using namespace std; 10 typedef long long ll; 11 const int maxn=1e5+5; 12 const int INF=0x3f3f3f3f; 13 string a; 14 int main() 15 { 16 int n,ans=0,temps=0,sumg=0,tempg=0; 17 cin>>n; 18 cin>>a; 19 for(int i=0;i<n;i++) 20 { 21 if(a[i]=='G') 22 { 23 sumg++; 24 tempg++; 25 } 26 else 27 { 28 temps=tempg; 29 tempg=0; 30 } 31 ans=max(ans,tempg+temps+1); 32 } 33 ans=min(ans,sumg); 34 printf("%d",ans); 35 return 0; 36 }