【Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies】暴力+细节题


B. Vova and Trophies

题意

给你一个只有G,S两种字符的字符串,可以交换一次两个位置的字符,问最终最长的连续的G可以有多少个
2 < = S < = 1 0 5 2<=|S|<=10^5

做法

有四种情况
第一种:只有一段连续的G,直接输出个数
第二种:有两段连续的G,两段间隔为1,答案为len1+len2
第三种:有两段连续的G,两段间隔大于1,答案为max(len1,len2)+1
第四种:有大于等于三段连续的G、并且有两段间隔等于一,答案等于所有间隔为一的两段中max(len1+len2)+1

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1e5+5;
vector<int> v1,v2;
char str[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",str+1);
    int pres=-1,preg=0;
    int maxx=0;
    int sumg=0;
    int maxxg=0;
    for(int i=1;i<=n;i++)
    {
        if(str[i]=='G')
        {
            int tmp=0;
            while(i<=n&&str[i]=='G')
            {
                tmp++;
                i++;
            }
            i--;
            if(pres==1) maxx=max(maxx,preg+tmp);
            preg=tmp;
            maxxg=max(maxxg,tmp);
            sumg++;
        }
        else
        {
            int tmp=0;
            while(i<=n&&str[i]=='S')
            {
                tmp++;
                i++;
            }
            i--;
            pres=tmp;
        }
    }
    int ans=0;
    if(sumg>=3) ans=max(ans,maxx+1);
    if(sumg>=2) ans=max(ans,maxx);
    if(sumg>=2) ans=max(ans,maxxg+1);
    ans=max(ans,maxxg);
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38891827/article/details/84648666