请教大神看下这种层次的家谱输出,如何使用线索树实现呢?C语言的问题

Problem Description
Kong belongs to a huge family. Recently he got a family name list which lists all men (no women) in his family over many generations.

The list shows that the whole family has a common ancestor, let's call him Mr. X. Of course, everybody except Mr.X in the list is Mr. X's descendant. Everybody's father is shown in the list except that Mr. X's father is not recorded. We define that Mr. X's generation number is 0. His son's generation number is 1.His grandson's generation number is 2, and so on. In a word, everybody's generation number is 1 smaller than his son's generation number. Everybody's generation number is marked in some way in the list.

Now Kong is willing to pay a lot of money for a program which can re-arrange the list as he requires ,and answer his questions such as how many brothers does a certain man have, etc. Please write this program for him.

Input
There are no more than 15 test cases.
For each test case:
The first line is an integer N( 1 <= N <= 30,000), indicating the number of names in the list.
The second line is the name of Mr. X.
In the next N-1 lines, there is a man's name in each line. And if the man's generation number is K, there are K dots( '.') before his name.

Please note that :
1) A name consists of only letters or digits( '0'-'9').
2) All names are unique.
3) Every line's length is no more than 60 characters.
4) In the list, a man M's father is the closest one above M whose generation number is 1 less than M.
5) For any 2 adjacent lines in the list, if the above line's generation number is G1 and the lower line' s generation number is G2, than G2 <= G1 +1 is guaranteed.

After the name list, a line containing an integer Q(1<=Q<=30,000) follows, meaning that there are Q queries or operations below.

In the Next Q lines, each line indicates a query or operation. It can be in the following 3 formats:
1) L
Print the family list in the same format as the input, but in a sorted way. The sorted way means that: if A and B are brothers(cousins don’t count), and A's name is alphabetically smaller than B's name, then A must appear earlier than B.
2) b name
Print out how many brothers does "name" have, including "name" himself.
3) c name1 name2
Print out the closest common ancestor of "name1" and "name2". "Closest" means the generation number is the largest. Since Mr. X has no ancestor in the list, so it's guaranteed that there is no question asking about Mr. X's ancestor.

The input ends with N = 0.

Output
Already mentioned in the input.

Sample Input
9
Kongs
.son1
..son1son2
..son1son1
...sonkson2son1
...son1son2son2
..son1son3
...son1son3son1
.son0

7
L
b son1son3son1
b son1son2
b sonkson2son1
b son1
c sonkson2son1 son1son2son2
c son1son3son1 son1son2
0

Sample Output
Kongs
.son0
.son1
..son1son1
...son1son2son2
...sonkson2son1
..son1son2
..son1son3
...son1son3son1
1
3
2
2
son1son1
son1

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

1
请教大神用dsp builder库搭建一个求余和查一维表该如何操作
2
pycharm中出现name 'reponse' is not defined错误怎么解决,请教大神,急????
1
请教大神如何让iframe框架自适应高度?
1
请教大神!!关于实现voip通信媒体代理服务器的问题
1
请教大神有关指针a,*a的区别?
0
请问各位大神,极限学习机用于预测是怎么有两个输出的呢??或者其他神经网络,是怎么构建两个输出的呢?
1
请教大神MYSQL GBK转换UTF8编码的问题
1
Centos7下键盘无法输入字母数字 回车退格都正常 是怎么回事 请教大神
2
一小段代码看不懂,请教大神
2
在mdk中用共用体,给它的成员赋值,然后报错了 请教大神怎么解决
3
请教大神,一个爬虫问题
0
大神求助啊,怎么把标签输出的内容用php输出呢?
7
高分悬赏,请教大神,Java语言输入30个数排序,怎么实现
1
Xshell6 乱码,麻烦大神看看
2
Java难题,大神进来协助,怎么用java实现99乘法表,输出出来要带换行
1
新手,求大神看看哪里有问题。输出输入的字符串中最长的单词
1
输出一个浮点数,输出浮点数绝对值,保留两位小数,但我这个程序为啥输入-9,输出-900,输出严重错误,请教各位大神
0
用MFC编写程序计算最后如何输出到框图,求大神都来帮忙看看了应该不是很难的一个问题的做法
2
请教各位大神,我看有的云服务器挺便宜的,想租一个1核2G的来初步使用,请问这种1核2G的能用来干什么啊。