题目链接:
http://codeforces.com/contest/1082/problem/B
B. Vova and Trophies
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 (2≤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 4 and 10. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 7.
In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4.
In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0.
题目大意:
给一串只含有 G 和 S 的字符串,有一次将两个字符对调位置的机会,求最长的连续 'G' 序列的长度。
思路:
直接贪心就好。。。。。
记录一个‘S'的前面和后面’G‘的数目,寻找最多的和,
最后判断,得到的最长连续G的数目需要<=字符串中G的数目,
移动S时,必须有另一个G和它换
This is the code:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x7f7f7f7f //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
int main()
{
int n;
string s;
cin>>n>>s;
int sum=0;//最长字串的shumu
int cnt1=0;//S前面的数目
int cnt2=0;//s后面的数目
int ans=0;//G的数目
for(int i=0;i<n;++i)
{
if(s[i]=='G')
{
++cnt2;
++ans;
}
else
{
cnt1=cnt2;
cnt2=0;
}
sum=max(sum,cnt2+cnt1+1);
}
sum=min(sum,ans);
printf("%d\n",sum);
return 0;
}