CodeForces 1082B

https://vjudge.net/problem/2065317/origin

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
10
GGGSGGGSGG
Output
7
Input
4
GGGG
Output
4
Input
3
SSS
Output
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.

题目的意思还是比较好理解的,就是你最多一次让S与G互换,题目要求你输出最长的一段G的连续的个数。

我写的比较麻烦,所以参考了其他的人代码,把代码改了改

#include<stdio.h>
#include<string.h>
char a[100005];
int sum[100005];
int max(int a,int b)
{
    if(a>b)
    return a;
    else
    return b;
}
int main()
{
    int n,j,k,sum1,Max;
    while(~scanf("%d",&n))
    {
        getchar();
        gets(a);
        j=0;sum1=0;k=0;
        for(int i=0;i<n;i++)//这个就是把一些连续的G的序列的长度记下来 
        {
            if(a[i]=='G')
            {
                j++;
                sum1++;
            }
            if(a[i]=='S'||i==n-1)
            {
                sum[k++]=j;//如果是GGSSGG,sum会是2,0,2;也就是有两个以上的S,中间才会有0; 
                j=0;
            }
        }
        /*for(int i=0;i<=k;i++)
        {
            if(i==k)
            printf("%d\n",sum[i]);
            else
            printf("%d ",sum[i]);
        }*/
        int ans=0;
        for(int i=1;i<=k;i++)
        {
            Max=sum[i]+sum[i-1];//这里就是把两个连续G序列的长度加起来,来看最长的 
            ans=max(ans,Max);//这是求最大的 ,这里有两种情况: 
            if(sum1>ans&&i==k)//这种是GGSGSGG,因为sum1>ans所以外面有多余的G与S互换,所以加一。 
            printf("%d\n",ans+1);
            if(sum1==ans&&i==k)//一种是GGSGG这样的,因为sum1==ans所以外面没有了G来与S互换  
            printf("%d\n",ans);
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/135jsk/p/10295409.html