CodeForces 626B

Description

Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:

  • take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
  • take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.

She repeats this process until there is only one card left. What are the possible colors for the final card?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.

The next line contains a string s of length n — the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.

Output

Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.

Sample Input

Input
2
RB
Output
G
Input
3
GRG
Output
BR
Input
5
BBBBB
Output

B

解体思路:分别统计出三种颜色的总个数,然后分成六种情况遍历一便,简单的bfs()

代码如下:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
struct stu{
	int b,g,r;//分别代表不同字母的个数 
};
stu star;
int vist[202][202][202];
int num[5];
char s[5];
int cmp(char a , char b){
	return a<b;
}
void bfs(){
	int i,j,k;
    k=0;
	stu temp;
	memset(vist,0,sizeof(vist));
	queue<stu>q;
	vist[star.b][star.g][star.r]=1;
	while(!q.empty()){
		q.pop();
	}
	q.push(star);
	while(!q.empty()){
		star=q.front();
		q.pop();
	 for(i=1;i<=6;i++){
		if(i==1){
		if(star.r>0&&star.g>0){
			temp.r=star.r-1;
			temp.g=star.g-1;
			temp.b=star.b+1;
		}
		else continue;
		}//表示rg
	    else if(i==2){
		if(star.b>0&&star.g>0){
			temp.b=star.b-1;
			temp.g=star.g-1;
			temp.r=star.r+1;
		}
		else continue;
		}
		//表示bg
		else if(i==3){
		if(star.r>0&&star.b>0){
			temp.r=star.r-1;
			temp.b=star.b-1;
			temp.g=star.g+1;
		}
		else continue;
		}//表示rb
		else if(i==4){
		  if(star.r>=2){
		  	temp.r=star.r-1;
		  	temp.g=star.g;
		  	temp.b=star.b;
		  }
		  else continue;
		}//表示rr
		else if(i==5){
		if(star.b>=2){
			temp.b=star.b-1;
			temp.r=star.r;
			temp.g=star.g;
		}
		else continue;
		} //表示bb 
		else if(i==6){
		   if(star.g>=2){
		   	temp.g=star.g-1;
		   	temp.b=star.b;
		    temp.r=star.r;
		   }
		   else continue;
		}//表示gg 
	if(vist[temp.b][temp.g][temp.r])continue;
	if(temp.b+temp.g+temp.r==1){
		if(temp.b==1){
			s[k]='B';
		}
		else if(temp.r==1){
			s[k]='R';
		}
		else s[k]='G';
		k++;
	}
	else{
		q.push(temp);
	}
	vist[temp.b][temp.g][temp.r]=1;
 }
 }
  sort(s,s+k,cmp);
  for(i=0;i<k;i++){
  	printf("%c",s[i]);
  }
  printf("\n");
}
int main(){
	int n,i;
	char c;
	while(scanf("%d",&n)!=EOF){
		getchar();
		num[1]=num[2]=num[3]=0;
		for(i=0;i<n;i++){
			scanf("%c",&c);
			if(c=='B'){
				num[1]++;
			}
			else if(c=='G'){
				num[2]++;
			}
			else if(c=='R'){
				num[3]++;
			}
		}
		if(n==1){
			printf("%c\n",c);
			continue;
		}
		star.b=num[1];
		star.g=num[2];
		star.r=num[3];
		bfs();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值