您的位置:首页 > 其它

CodeForces 626B

2016-03-07 22:06 211 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: