标签:
3 2 ABC CDE EFG FA BE 0 0
great-grandparent -
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN=105; int son[MAXN]; int dep[MAXN]; int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(son,-1,sizeof(son)); memset(dep,0,sizeof(dep)); for(int i=0;i<n;i++) { scanf("%*c"); char a,b,c; scanf("%c%c%c",&a,&b,&c); a-=‘A‘; if(b!=‘-‘) { b-=‘A‘; son[b]=a; dep[b]=dep[a]+1; } if(c!=‘-‘) { c-=‘A‘; son[c]=a; dep[c]=dep[a]+1; } } while(m--) { scanf("%*c"); char a,b; scanf("%c%c",&a,&b); a-=‘A‘; b-=‘A‘; int l=0; int start,goal; int up; if(dep[a]>dep[b]) { up=true; start=a; goal=b; } else { up=false; start=b; goal=a; } int tmp=start; bool mark=false; while(son[tmp]!=-1) { tmp=son[tmp]; l++; if(tmp==goal) { mark=true; break; } } if(mark==false) { printf("-\n"); } else { if(up==true) { if(l==1) { printf("parent\n"); } else if(l==2) { printf("grandparent\n"); } else { for(int i=l;i>2;i--) { printf("great-"); } printf("grandparent\n"); } } else { if(l==1) { printf("child\n"); } else if(l==2) { printf("grandchild\n"); } else { for(int i=l;i>2;i--) { printf("great-"); } printf("grandchild\n"); } } } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5402372.html