Every person's blood has 2 markers called ABO alleles. Each of the markers is represented by one of three letters: A, B, or O. This gives six possible combinations of these alleles that a person can have, each of them resulting in a particular ABO blood type for that person.
Combination | ABO Blood Type |
AA | A |
AB | AB |
AO | A |
BB | B |
BO | B |
OO | O |
Likewise, every person has two alleles for the blood Rh factor, represented by the characters + and -. Someone who is ``Rh positive" or ``Rh+" has at least one + allele, but could have two. Someone who is ``Rh negative" always has two - alleles.
The blood type of a person is a combination of ABO blood type and Rh factor. The blood type is written by suffixing the ABO blood type with the + or - representing the Rh factor. Examples include A+, AB-, and O-.
Blood types are inherited: each biological parent donates one ABO allele (randomly chosen from their two) and one Rh factor allele to their child. Therefore 2 ABO alleles and 2 Rh factor alleles of the parents determine the child's blood type. For example, if both parents of a child have blood type A-, then the child could have either type A- or type O- blood. A child of parents with blood types A+ and B+ could have any blood type.
In this problem, you will be given the blood type of either both parents or one parent and a child; you will then determine the (possibly empty) set of blood types that might characterize the child or the other parent.
Note: an uppercase letter ``Oh" is used in this problem to denote blood types, not a digit (zero).
Input
The input consists of multiple test cases. Each test case is on a single line in the format: the blood type of one parent, the blood type of the other parent, and finally the blood type of the child, except that the blood type of one parent or the child will be replaced by a question mark. To improve readability, whitespace may be included anywhere on the line except inside a single blood type specification.
The last test case is followed by a line containing the letters E, N, and D separated by whitespace.
Output
For each test case in the input, print the case number (beginning with 1) and the blood type of the parents and the child. If no blood type for a parent is possible, print ``IMPOSSIBLE". If multiple blood types for parents or child are possible, print all possible values in a comma-separated list enclosed in curly braces. The order of the blood types inside the curly braces does not matter.
The sample output illustrates multiple output formats. Your output format should be similar.
Sample Input
O+ O- ? O+ ? O- AB- AB+ ? AB+ ? O+ E N D
Sample Output
Case 1: O+ O- {O+, O-} Case 2: O+ {A-, A+, B-, B+, O-, O+} O- Case 3: AB- AB+ {A+, A-, B+, B-, AB+, AB-} Case 4: AB+ IMPOSSIBLE O+
#include<stdio.h>
const int ABO[4][4][4]={1,0,0,1, 1,1,1,1, 1,1,1,0, 1,0,0,1,
1,1,1,1, 0,1,0,1, 1,1,1,0, 0,1,0,1,
1,1,1,0, 1,1,1,0, 1,1,1,0, 1,1,0,0,
1,0,0,1, 0,1,0,1, 1,1,0,0, 0,0,0,1};
const int Rh[2][2][2]={1,1, 1,1, 1,1, 0,1};
char ch;
int cases,i,j,k,l,father,mother,child;
int ans_ABO[4],ans_Rh[2];
void out(int x,int type)
{
if(type==0)
printf(" ");
else if(type==1)
printf(" {");
else
printf(", ");
if(x==1||x==-1)
printf("A");
if(x==2||x==-2)
printf("B");
if(x==3||x==-3)
printf("AB");
if(x==4||x==-4)
printf("O");
if(x>0)
printf("+");
else
printf("-");
}
int get()
{
int tmp;
scanf("%c",&ch);
while(ch!='A'&&ch!='B'&&ch!='O'&&ch!='E'&&ch!='N'&&ch!='D'&&ch!='?')
scanf("%c",&ch);
if(ch=='E'&&ch=='N'&&ch=='D')
return 0;
if(ch=='?')
return 5;
if(ch=='A')
tmp=1;
else if(ch=='B')
tmp=2;
else
tmp=4;
scanf("%c",&ch);
if(ch=='+')
return tmp;
else if(ch=='-')
return -tmp;
else
tmp=3;
scanf("%c",&ch);
if(ch=='+')
return tmp;
else
return -tmp;
}
void write()
{
int t;
k=0;l=0;
for(i=0;i<4;i++)
k+=ans_ABO[i];
for(i=0;i<2;i++)
l+=ans_Rh[i];
if(k*l==0)
printf(" IMPOSSIBLE");
if(k*l==1)
for(i=0;i<4;i++)
if(ans_ABO[i])
for(j=0;j<2;j++)
if(ans_Rh[j])
{
if(j==0)
out(i+1,0);
else
out(-i-1,0);
}
if(k*l>1)
{
t=0;
for(i=0;i<4;i++) if(ans_ABO[i])
for(j=0;j<2;j++) if(ans_Rh[j])
{
t++;
if(j==0)
out(i+1,t);
else
out(-i-1,t);
}
printf("}");
}
}
int main()
{
while(1)
{
father=get();
mother=get();
child=get();
if(father==0)
break;
if(father==5)
{
if(mother>0)
k=mother-1;
else
k=-1-mother;
if(child>0)
l=child-1;
else
l=-1-child;
for(i=0;i<4;i++)
ans_ABO[i]=ABO[i][k][l];
if(mother>0)
k=0;
else
k=1;
if(child>0)
l=0;
else
l=1;
for(i=0;i<2;i++)
ans_Rh[i]=Rh[i][k][l];
}
if(mother==5)
{
if(father>0)
k=father-1;
else
k=-1-father;
if(child>0)
l=child-1;
else
l=-1-child;
for(i=0;i<4;i++)
ans_ABO[i]=ABO[k][i][l];
if(father>0)
k=0;
else
k=1;
if(child>0)
l=0;
else
l=1;
for(i=0;i<2;i++)
ans_Rh[i]=Rh[k][i][l];
}
if(child==5)
{
if(father>0)
k=father-1;
else
k=-1-father;
if(mother>0)
l=mother-1;
else
l=-1-mother;
for(i=0;i<4;i++)
ans_ABO[i]=ABO[k][l][i];
if(father>0)
k=0;
else
k=1;
if(mother>0)
l=0;
else
l=1;
for(i=0;i<2;i++)
ans_Rh[i]==Rh[k][l][i];
}
printf("Case %d:",++cases);
if(father!=5)
out(father,0);
else
write();
if(mother!=5)
out(mother,0);
else
write();
if(child!=5)
out(child,0);
else
write();
printf("\n");
}
return 0;
}