B. Vova and Trophies

B. Vova and Trophies
B. Vova and Trophies
题意自己总结了一下就是
给一串只含有 G 和 S 的字符串,有一次将两个字符对调位置的机会,求最长的连续 ‘G‘ 序列的长度。
当时做题的时候交了两发才过,哎,还是太弱了,要自闭了。嘤嘤嘤;
一开始用了二分搜索,debug到爆炸。其实就是一个贪心:记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 ‘G‘ 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen。也就是说找一下连续的G串有多少串sumg并记录最大值maxx1,然后再找 如果一个S的左边和右边都是G串那么再记录一下此类串的最大长度maxx2
如果sumg1输出maxx1
如果sumg
2输出max(maxx1+1,maxx2);
如果sumg>2输出max(maxx1+1,maxx2+1);

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
char a[maxn];
int main()
{
    int n,sumg=0,tempg=0;
    scanf("%d",&n);
    getchar();
    scanf("%s",a);
    int maxx1=0,maxx2=0,temps=0,flag=0,cnt=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]=='G')
        {
            sumg++;
            tempg++;
            if(i==n-1)
            {
                if(temps)
                    maxx1=max(maxx1,temps+tempg);
                else
                    maxx2=max(maxx2,tempg);
                cnt++;
            }
        }
        else
        {
            if(tempg)
    cnt++;
            if(flag==1)
            {
                maxx1=max(tempg+temps,maxx1);
                temps=tempg;
                tempg=0;
                flag=1;
            }
            if(a[i+1]!='G')
            {
                maxx2=max(maxx2,tempg);
                tempg=0;
                temps=0;
            }
            else
            {
                if(tempg&&a[i-1]=='G')
                    flag=1;
                temps+=tempg;
                tempg=0;
            }
        }
    }
    if(sumg==n)
        printf("%d\n",n);
    else if(sumg)
    {
        if(cnt==2)
        {
            printf("%d\n",max(maxx1,maxx2+1));
        }
        else if(cnt==1)
        {
            printf("%d\n",maxx2);
        }
        else
        {
            printf("%d\n",max(maxx1+1,maxx2+1));
        }
    }
    else
        printf("0\n");
    return 0;
}