H-世
https://vjudge.net/problem/2065317/originVova has won n 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 n (2≤n≤105) — the number of trophies.
The second line contains n characters, each of them is either G or S. If the i-th character is G, then the i-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
10
GGGSGGGSGG
Output
7
Note
In the first example Vova has to swap trophies with indices 4 and 10. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 7.
题目大意
一水题,就是题目给出一串字符,只有G和S,只能交换一次任意两个字符,问交换之后的字符串之中G的最长的不中断的字符串是多长。
//最初的代码,我将所有的情况都算出之后在求最大,比较复杂,细节有点头大,不推荐使用
#include<stdio.h>
#include<string.h>
int n,k,index,sum,b[100005],c[100005];
char a[100005];
int MAX(int a,int b)
{
return a>b?a:b;
}
int main()
{
while(~scanf("%d",&n))
{
k=sum=index=0;
getchar();
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(int i=0;i<n;i++)
{
scanf("%c",&a[i]);
if(a[i]=='G') {
if(a[i-1]=='S') k++;
b[k]++;
}
else if(a[i]=='S')
k++;
if(a[i]=='S'&&i==n-1) k++;
}
if(b[k]>0) k++;
int ans=0;
for(int i=0;i<k;i++)
{
if(b[i]>0) sum++;
}
for(int i=0;i<k;i++)
{
if(b[i]>0) {
if(i+2<k) {
i+=2;
if(b[i]==0) {
if(sum>1)
c[index]=b[i-2]+1;
else
c[index]=b[i-2];
}
else {
if(sum>2)
c[index]=b[i-2]+b[i]+1;
else
c[index]=b[i-2]+b[i];
}
i-=2;
}
if(i-2>=0) {
i=i-2;
if(b[i]==0) {
if(sum>1)
c[index]=MAX(b[i+2]+1,c[index]);
else
c[index]=MAX(b[i+2],c[index]);
}
else {
if(sum>2)
c[index]=MAX(b[i+2]+b[i]+1,c[index]);
else
c[index]=MAX(b[i+2]+b[i],c[index]);
}
i+=2;
}
if(i+2>=k&&i-2<0) c[index]=b[i];
}
index++;
}
for(int i=0;i<index;i++)
{
if(c[i]>ans) ans=c[i];
}
printf("%d\n",ans);
}
return 0;
}