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?
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.
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.
2 RB
G
3 GRG
BR
5 BBBBB
B
In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.
In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card.
In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.
思路:
就是给你三种颜色挑选其中的任意两个能够换成第三种颜色,也可以将两个相同的颜色换成一个同种颜色的,然后让你输出所有的可能(最终就只剩下一种颜色了!)(按照字典序输出所有的可能!)
规律题!当就一种颜色的时候直接输出,如果有三种颜色的话,就有三种可能,按照字典序输出的话就是(BGR),如果有两种颜色的话,就是分情况了!1.如果就输入两个字符,那么就分三种情况输出;2.如果输入的字符个数大于2,那么看看如果其中一个字符的个数是1的话就又分6种情况;3.如果输入字符的个数大于2,且都是大于等于2的个数,那么就输出BGR;
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char a[205];
char b[205];
int cnt;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int t[4];
memset(t,0,sizeof(t));
cnt=1;
scanf("%s",a);
sort(a,a+n);
b[cnt-1]=a[0];
for(int i=0;i<n-1;i++)
{
if(a[i]!=a[i+1])
{
b[cnt]=a[i+1];
t[cnt-1]=i+1;
cnt++;
}
}
t[cnt-1]=n-t[cnt-2];
if(cnt==1)
{
printf("%c\n",a[0]);
continue;
}
else if(cnt==3)
{
printf("BGR\n");
continue;
}
else if(cnt==2)
{
if(n==2)//
{
if(b[0]=='B'&&b[1]=='G')
printf("R\n");
else if(b[0]=='B'&&b[1]=='R')
printf("G\n");
else
printf("B\n");
}
else if(t[0]==1||t[1]==1)//
{
if(b[0]=='B'&&b[1]=='G'&&t[0]==1)
{
printf("BR\n");
}
else if(b[0]=='B'&&b[1]=='R'&&t[0]==1)
{
printf("BG\n");
}
else if(b[0]=='B'&&b[1]=='G'&&t[1]==1)
{
printf("GR\n");
}
else if(b[0]=='G'&&b[1]=='R'&&t[0]==1)
{
printf("BG\n");
}
else if(b[0]=='B'&&b[1]=='R'&&t[1]==1)
{
printf("GR\n");
}
else if(b[0]=='G'&&b[1]=='R'&&t[1]==1)
{
printf("BR\n");
}
}
else
{
printf("BGR\n");
}
}
}
return 0;
}