标签:
题目地址:http://ac.jobdu.com/problem.php?pid=1035
题目1035:找出直系亲属
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2388 解决:939
3 2 ABC CDE EFG FA BE 0 0
great-grandparent -
#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 && 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; }
标签:
原文地址:http://www.cnblogs.com/moxiaopeng/p/5024559.html