Title Address: http://ac.jobdu.com/problem.php?pid=1035
Topic 1035: Identifying immediate family members
time limit:1 seconds memory limit:32 trillion special sentence: no submission:2388 Resolution:939
-
Title Description:
-
If a, B is the parent of C, then a A A, B is the parent,c of C is a A, a A, if a is a, C is the (outside) grandfather, grandmother, then a B is the grandparent,c of C is a A, B is the grandchild, if A is C (outside) great grandfather, Once grandmother, then A, B is the great-grandparent,c of C is a, b great-grandchild, then more than one generation, then add a great-on the relationship.
-
Input:
-
The input contains multiple sets of test cases, each of which consists of 2 integers n (0<=n<=26) and M (0<M<50), each representing n kinship and M questions, followed by a string of n lines in the form of ABC, which indicates that A's parents are B and C, respectively. If A's parental information is incomplete, then use-instead, for example A-c, then the M-line form of the FA string, which indicates the relationship between F and a.
When N and m are 0 o'clock end input.
-
Output:
-
If the 2 persons questioned are immediate family members, please describe the relationship of output 2 according to the topic, if there is no direct relationship, please output-.
Refer to the example for the specific meaning and output format.
-
Sample input:
-
3 2ABCCDEEFGFABE0 0
-
Sample output:
-
great-grandparent-
-
Source:
- 2009 Zhejiang University computer and software engineering research on the real problem of life test
-
Answering:
-
Problem solving problems? to discuss the subject of discussion please visit: http://t.jobdu.com/thread-7759-1-1.html
-
Code:
#include <cstdio> #include <vector>using namespace Std;int do_find_relation_child (char A, char B, const vector<char> &vec, int cur) {if (a = = '-' | | b = = '-') {return 0; } if (a = = b) {return cur; } return Do_find_relation_child (Vec[a-' a '], B, VEC, cur + 1);} void Get_relation (char A, char B, const vector<char> &VEC) {bool Parent_flag = true; int r = Do_find_relation_child (A, B, VEC, 0); if (r = = 0) {r = Do_find_relation_child (b, A, VEC, 0); Parent_flag = false; } if (r = = 0) {printf ("-\n"); } else {if (R > 2) {for (int i = 3; I <= R; ++i) {printf ("great-"); }} if (R > 1) {printf ("grand"); } if (Parent_flag) {printf ("parent\n"); } else {printf ("child\n"); }}}int Main () {int n = 0, m = 0; while (scanf ("%d%d\n", &n, &m)! = EOF && m > 0 &Amp;& n > 0) {vector<char> Vec (26, '-'); for (int i = 0; i < n; ++i) {char a,b,c; scanf ("%c%c%c\n", &a, &b, &c); if (b! = '-') {vec[b-' a '] = A; } if (c! = '-') {vec[c-' a '] = A; }} for (int i = 0; i < m; ++i) {char A, B; scanf ("%c%c\n", &a, &b); Get_relation (A, B, VEC); }} return 0;}
Topic 1035: Identifying immediate family members