1. 程式人生 > >【Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies】暴力+細節題

【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; }