3 2
ABC
CDE
EFG
FA
BE
0 0
great-grandparent
-
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
char cmp(char a,char b){
	return a<b?0:1;
}
int main(){
	int m,n;
	while(cin>>m>>n&&(m!=0&&n!=0)){
		string str[m];
		for(int i=0;i<m;i++){
	  	 cin>>str[i];
		}
		char wt[n][2];
		for(int i=0;i<n;i++){
			for(int j=0;j<2;j++){
				cin>>wt[i][j];
			}
		}
		for(int i=0;i<n;i++){
			int temp=0,index=0;
			int j=m-1;
			int count=0;
			if(wt[i][1]<wt[i][0]){index=1;};
			sort(wt[i],wt[i]+2,cmp);
			while(j>=0){
	    		if(str[j].find(wt[i][0])!=string::npos){
	    			count++;
		  		    wt[i][0]=str[j][0];
	    		}
	    		if(wt[i][1]==str[j][0]) {temp=j;break;}
	   		 	j--;
			}
			if(wt[i][1]!=str[temp][0]) count=0;
			if(index==1){ 
				while(count>2){
					cout<<"great-"; 
					count--;
				}
				if(count==2)
					cout<<"grandparent"<<endl;
				if(count==1)
					cout<<"parent"<<endl;
				if(count==0)
					cout<<"-"<<endl;
			}
    		else{
    			while(count>2){
					cout<<"great-"; 
					count--;
				}	
				if(count==2)
					cout<<"grandchild"<<endl;
				if(count==1)
					cout<<"child"<<endl;
				if(count==0)
					cout<<"-"<<endl;
    		}
		}
	}
	return 0;
}
 原文:http://www.cnblogs.com/lchzls/p/5847570.html