poj 1577 Falling Leaves

题目链接:http://poj.org/problem?id=1577

这也可以算是这次周赛最简单的题了,可是发现的太晚了,再有一分钟时间就好了。

题意是:给你一些叶子节点,然后去掉叶子节点继续找叶子节点。而且所有节点的左子树小于它,右子树大于它,最后让你输出中序遍历。

也就是对于每组数据有最后一组数据到第一组数据建树。树建成输出就ok了。

View Code
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 using namespace std;
6 struct ss{
7 char c;
8 int lchild;
9 int rchild;
10 }num[30];
11 int N;
12 char str[30][27];
13 void add(int t,char c)
14 {
15 if(c<num[t].c)
16 {
17 if(num[t].lchild==0)
18 {
19 num[N].c=c;
20 num[t].lchild=N++;
21 return ;
22 }
23 else add(num[t].lchild,c);
24 }
25 else {
26 if(num[t].rchild==0)
27 {
28 num[N].c=c;
29 num[t].rchild=N++;
30 return ;
31 }
32 else add(num[t].rchild,c);
33 }
34 }
35 void print(int t)
36 {
37 printf("%c",num[t].c);
38 if(num[t].lchild)print(num[t].lchild);
39 if(num[t].rchild)print(num[t].rchild);
40 }
41 int main()
42 {
43 int i,j,cont1,len;
44 while (scanf("%s",str[0])!=EOF)
45 {
46 memset(num,0,sizeof(num));
47 if(str[0][0]=='$')break;
48 cont1=0;
49 while (str[cont1][0]>='A'&&str[cont1][0]<='Z')
50 {
51 scanf("%s",str[++cont1]);
52 }
53 cont1--;
54 N=0;
55 num[N++].c=str[cont1][0];
56 for (i=cont1-1;i>=0;i--)
57 {
58 len=strlen(str[i]);
59 for (j=0;j<len;j++)
60 {
61 add(0,str[i][j]);
62 }
63 }
64 print(0);
65 printf("\n");
66 }
67 return 0;
68 }



posted @ 2012-03-30 12:12  我们一直在努力  阅读(408)  评论(0编辑  收藏  举报