日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?(2≤n≤1052≤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.

題目大意:

? ??給定長度為n的字符串s,字符串中只有G和S,只允許最多一次操作:任意位置的兩個字符互換。求連續G的最長長度。

解題報告:

? ?深夜掉分2333、、、(C明顯**題啊咋就沒敢寫,,大概還是AB用的時間太長了,還是苔菜)

? ?這題看似很好實現但是其實還是需要進行一些思維的轉化才能做的。用了一堆數組,L和R數組分別表示截止當前的前綴和后綴中是否出現過G。pre代表截止當前的連續G串,hou代表截止當前的后綴G串。S數組代表字符S出現過的位置的記錄。

? 數組設定好之后就可以實現了。。首先啊,,答案肯定由兩種情況組成(其中取個最大值就可以了)。一種是連續G串。一種是一段G串中間帶個S。對于第一種比較好處理。對于第二種,又分兩種情況,一種是還有多余的G可以填補這個S,一種是沒有多余的G了,,只能用兩邊的一個G來填補這個S。。所以代碼就出來了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; char s[MAX]; int pre[MAX],hou[MAX]; int S[MAX]; bool L[MAX],R[MAX]; int main() {int n,cnt=0,tot=0;cin>>n;cin>>(s+1);for(int i = 1; i<=n; i++) {if(s[i] == 'G' || L[i-1] == 1) L[i] =1;else L[i]=0;}for(int i = n; i>=1; i--) {if(s[i] == 'G' || R[i+1] == 1) R[i] = 1;else R[i] = 0;}for(int i = 1; i<=n; i++) {if(s[i] == 'G') cnt++;else S[++tot] = i;}for(int i = n; i>=1; i--) {if(s[i] == 'G') hou[i] = hou[i+1] + 1;else hou[i] = 0;}int maxx = -1,tmp=0;for(int i = 1; i<=n; i++) {if(s[i] == 'G') pre[i] = pre[i-1]+1,tmp++;else {pre[i]=0;maxx = max(maxx,tmp);tmp=0;}}maxx = max(tmp,maxx);int ma = 0,flag = 0;//flag=0代表沒有使用過S tmp=0;int tmp1 = 0;for(int i = 1; i<=tot; i++) {int cur = pre[S[i]-1] + hou[S[i]+1];if(cur < cnt && (L[S[i]-1] || R[S[i]+1])) ma = max(ma,cur+1);if(cur == cnt) ma = max(ma,cur);}// for(int i = 1; i<=n; i++) { // if(flag == 1 && s[i] == 'S') { // ma = max(ma,tmp);tmp=tmp1;flag=0; // } // if(s[i] == 'G') tmp++,tmp1++; // else if (s[i] == 'S') { // flag=1;tmp1 = 0; // tmp++; // } // } // ma = max(ma,tmp); // if(cnt <= ma) ma=-1; printf("%d\n",max(maxx,ma));return 0 ;} /* 5 SGGSG5 GGGSG

總結:

? ?看連續G的個數的時候一定別忘了還有這一套那就是執行完了之后maxx = max(tmp,maxx),,因為有可能進行到末尾了,,這個tmp還沒賦值給maxx。。

總結

以上是生活随笔為你收集整理的【CodeForces - 1082B】Vova and Trophies (贪心模拟,暴力)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。