天天看点

CodeForces - 1082B 思维题 不断更新

CodeForces - 1082B,不断更新。

##链接: CodeForces - 1082B

##题目:

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.

Examp:

Input

10

GGGSGGGSGG

Output

7

Input

4

GGGG

Output

4

Input

3

SSS

Output

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.

##题意:

金奖杯和银奖杯排成一排,要求交换金奖杯和银奖杯一次,使排在一起的金奖杯尽可能长,求金奖杯最长的长度。

##题目分析

这个题是思维题,鄙人在做这道题的时候,想的是分类,但是分来分去,自己都分迷糊了,情况很多,如果要分类就要把所有的情况都弄清楚,而且代码写的很长。

既然是思维题,那么肯定是锻炼考验你思维的好时机,所以开动你的脑子好好想想,观察,动手,思考。那么现在就开始想想吧!(仔细想想,一定要养成独立思考的习惯。自己必须独自去做,才能有所成长,如果一直依赖,一直生活在庇护之下,是长不大的,要自立自强。学习也一样,如果一遇到问题就找答案,不独立思考,怎么提高)

想好了吗?现在就来讲一下思路吧!

记录‘S’左面和右面的‘G’的数量,做和。取最大值maxg。记录‘G’的数量count,如果maxg大于count输出count,反之输出maxg。

就是一直更新maxg。

##代码

#include<stdio.h>
int max(int a,int b)
{
	if(a > b)
	return a;
	else
	return b;
}
int main()
{
	int n;
	int count = 0,left = 0,right = 0,maxg = 0;
	char a[100010];
	scanf("%d",&n);
	getchar();
	gets(a);
	for(int  i = 0;i < n;i ++)
	{
		if(a[i] == 'G')
		{
			count ++;
			right ++;
		}
		else
		{
			left = right;
			right = 0;
		}
		maxg = max(maxg,left + right + 1);
	}
	if(maxg > count)
	printf("%d\n",count);
	else
	printf("%d\n",maxg);
	return 0; 
}