1 second
Memory limit:32 MB
Special Judgment:No
Submit:1892
Solution:764
-
Description:
-
If A and B are the parents of C, A and B are the parents of C, and C is the child of a and B. If a and B are the grandfather and grandmother of C, then A and B are the grandparent of C, and C is the grandchild of A and B. If a and B are the grandfathers of C, then, B is the great-grandparent of C, and C is the great-grandchild of A and B. If there are more than one generation, a great-is added to the relationship -.
-
Input:
-
The input contains multiple test cases. Each test case contains two integers, n (0 <= n <= 26) and M (0 <m <50 ), it indicates that there are n relatives and M problems, and then the following is a string of N rows, such as ABC, indicating that the parents of a are B and C, respectively, if the parent information of a is incomplete, replace it with-, such as the A-C, and then the M-row form, such as the FA string, to ask about the relationship between F and.
End input when N and m are 0.
-
Output:
-
If the two members are immediate relatives, output the relationship between the two according to the description of the question. If there is no immediate relationship, output -.
For specific meanings and output formats, see the example.
-
Sample input:
-
3 2ABCCDEEFGFABE0 0
-
Sample output:
-
Great-grandparent-
The code is supplemented later:
# Include <stdio. h> using namespace STD; int tree [26]; char Buf [4]; int findd (int A, int B) {// query the relationship between A and B, int CNT = 1; int flag = 1; // 1 indicates that A and B are in the same genealogy, and 0 indicates that there is no relationship between while (B! = Tree [a]) {if (a = tree [a]) {// The root node has been reached. Flag = 0; break;} is irrelevant to A and B ;} A = tree [a]; // The parent node of A (for children) CNT ++;} If (flag = 0) {return 0 ;} else {return CNT ;}} int main (void) {int n, m; while (scanf ("% d", & N, & M )! = EOF) {If (n = 0 & M = 0) {break;} // initialize for (INT I = 0; I <26; ++ I) {tree [I] = I;} while (n --) {scanf ("% s", Buf); int A = Buf [0]-'A '; tree [Buf [1]-'a'] = A; tree [Buf [2]-'a'] = A; // set parent node} while (M --) {scanf ("% s", Buf); int x = Buf [0]-'A'; int y = Buf [1]-'A '; int z = findd (x, y)-findd (Y, x); // there must be a value of 0, and two minus signs are used to determine whether the child is a parent if (Z = 0) {// Description: printf ("-\ n");} else if (Z = 1) {printf ("parent \ n ");} else if (Z = 2) {printf ("grandparent \ n");} else if (z> 2) {While (Z-2) {printf ("Great -"); Z --;} printf ("grandparent \ n");} else if (Z =-1) {printf ("Child \ n ");} else if (Z =-2) {printf ("grandchild \ n");} else {While (-z)-2) {z ++; printf ("Great-");} printf ("grandchild \ n") ;}} return 0 ;}
Question 1035: finding immediate relatives