Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))

Posted zero-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))相关的知识,希望对你有一定的参考价值。

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
10
GGGSGGGSGG
output
Copy
7
input
Copy
4
GGGG
output
Copy
4
input
Copy
3
SSS
output
Copy
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.

 

在左右两侧都是G,中间为S的时候,在最后需要和直接连续G的长度交换一个S为G的比较一下,wa在这里了。

 

代码:

 1 //B
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e5+10;
 6 const int inf=0x3f3f3f3f;
 7 
 8 char c[maxn];
 9 vector<int> ve;
10 
11 int main()
12 {
13     int n;cin>>n;
14     cin>>c;
15     int num=0;
16     for(int i=0;i<n;i++){
17         if(c[i]==G) num++;
18         if(c[i-1]==G&&c[i]==S&&c[i+1]==G) ve.push_back(i);
19     }
20     int ret=0,tmp=0;
21     for(int i=0;i<n;i++){
22         if(c[i]==G) tmp++;
23         else ret=max(ret,tmp),tmp=0;
24     }
25     ret=max(ret,tmp);
26     ret=min(ret+1,num);
27     if(num==n) {cout<<n<<endl;return 0;}
28     if(num==0) {cout<<0<<endl;return 0;}
29     if(ve.size()==0){
30         int ans=-1,cnt=0;
31         for(int i=0;i<n;i++){
32             if(c[i]==G) cnt++;
33             if(c[i]==S) ans=max(ans,cnt),cnt=0;
34         }
35         ans=max(ans,cnt);
36         cout<<min(num,ans+1)<<endl;
37     }
38     else{
39         int ans=0,flag=0;
40         for(auto it:ve){
41             int cnt=0;
42             for(int i=it-1;i>=0;i--){
43                 if(c[i]==S) break;
44                 else cnt++;
45             }
46             for(int i=it+1;i<n;i++){
47                 if(c[i]==S) break;
48                 else cnt++;
49             }
50             ans=max(ans,cnt);
51         }
52         ans=min(num,ans+1);
53         cout<<max(ans,ret)<<endl;
54     }
55 }
56 
57 /*
58 16
59 GSGSSGSSGGGSSSGS
60 
61 4
62 */

 

 

 

 

以上是关于Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1082B Vova and Trophies 模拟,水题,坑 B

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies

Codeforces Round #515 (Div. 3) 解题报告(A~E)

Codeforces 1324E - Sleeping Schedule

Codeforces.1082E.Increasing Frequency(思路)

CodeForces-1082E Increasing Frequency