http://ac.jobdu.com/problem.php?pid=1035
来源:互联网 发布:movemake软件下载 编辑:程序博客网 时间:2024/04/28 11:32
- 题目描述:
- 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-。
- 输入:
- 输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50), 分别表示有n个亲属关系和m个问题, 然后接下来是n行的形式如ABC的字符串,表示A的父母亲分别是B和C,如果A的父母亲信息不全,则用-代替,例如A-C,再然后是m行形式如FA的字符串,表示询问F和A的关系。
当n和m为0时结束输入。
- 输出:
- 如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。
具体含义和输出格式参见样例.
- 样例输入:
3 2ABCCDEEFGFABE0 0
- 样例输出:
great-grandparent-
一次AC,哈哈- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <memory.h>
- #include <cstdio>
- #include <cstdlib>
- #include <vector>
- using namespace std;
- int exist[27];
- struct Node{
- char parent1, parent2;
- int exist;
- };
- Node nodes[27];
- int ans;
- void find(char a, char b, int i){
- if(!exist[b-'A'])
- return;
- Node node = nodes[b-'A'];
- if(node.parent1==a || node.parent2==a){
- ans = i;
- return;
- }else{
- find(a, node.parent1, i+1);
- find(a, node.parent2, i+1);
- }
- }
- int main(){
- //freopen("in.txt", "r", stdin);
- int n, m;
- char ch, p1, p2;
- char a,b;
- while(cin>>n>>m, n+m){
- memset(exist, 0, sizeof(exist));
- for(int i=0;i<n;++i){
- scanf(" %c%c%c", &ch, &p1, &p2);
- nodes[ch-'A'].parent1 = p1;
- nodes[ch-'A'].parent2 = p2;
- exist[ch-'A'] = 1;
- }
- for(int i=0;i<m;++i){
- scanf(" %c%c", &a, &b);
- ans = -1;
- find(a, b, 0);
- if(ans!=-1){
- while(ans>=2){
- printf("great-");
- ans--;
- }
- if(ans==1){
- printf("grand");
- ans--;
- }
- if(ans==0)
- printf("parent");
- }else{
- find(b, a, 0);
- if(ans!=-1){
- while(ans>=2){
- printf("great-");
- ans--;
- }
- if(ans==1){
- printf("grand");
- ans--;
- }
- if(ans==0)
- printf("child");
- }else{
- printf("-");
- }
- }
- printf("\n");
- }
- }
- //fclose(stdin);
- }
- http://ac.jobdu.com/problem.php?pid=1035
- http://ac.jobdu.com/problem.php?pid=1008
- http://ac.jobdu.com/problem.php?pid=1009
- http://ac.jobdu.com/problem.php?pid=1007
- http://ac.jobdu.com/problem.php?pid=1033
- http://ac.jobdu.com/problem.php?pid=1005
- http://ac.jobdu.com/problem.php?pid=1006
- http://ac.jobdu.com/problem.php?pid=1003
- http://ac.jobdu.com/problem.php?pid=1032
- http://ac.jobdu.com/problem.php?pid=1030
- http://ac.jobdu.com/problem.php?pid=1029
- http://ac.jobdu.com/problem.php?pid=1028
- http://ac.jobdu.com/problem.php?pid=1027
- http://ac.jobdu.com/problem.php?pid=1026
- http://ac.jobdu.com/problem.php?pid=1025
- http://ac.jobdu.com/problem.php?pid=1024
- http://ac.jobdu.com/problem.php?pid=1023
- http://ac.jobdu.com/problem.php?pid=1022
- http://ac.jobdu.com/problem.php?pid=1006
- ARM处理器各个模式之间是如何切换的?ARM各个模式之间切换时,上下文的保存哪些是硬件在做?哪些是操作系统在做?
- http://ac.jobdu.com/problem.php?pid=1003
- win7下安装Ubuntu16.04双系统
- 分页算法
- http://ac.jobdu.com/problem.php?pid=1035
- 星号图
- http://ac.jobdu.com/problem.php?pid=1032
- http://ac.jobdu.com/problem.php?pid=1030
- 虚拟机和主机文件共享的四种方法
- Windows7快捷键大全
- http://ac.jobdu.com/problem.php?pid=1029
- Android中Fragment-SDK翻译版
- windows phone 7开发环境