uva10562

题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1503

是树,递归即可

学习一下fgets的用法http://blog.csdn.net/daiyutage/article/details/8540932

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cctype>
 4 using namespace std;
 5 
 6 const int maxn=210;
 7 int n;
 8 char s[maxn][maxn];
 9 //递归遍历并且输出以字符串s[r][c]为根的树
10 void dfs(int r,int c)
11 {
12     printf("%c(",s[r][c]);
13     if(r+1<n&&s[r+1][c]=='|') //有子树
14     {
15         int i=c;
16         while(i-1>=0&&s[r+2][i-1]=='-') i--; //找"---"的左边界
17         while(s[r+2][i]=='-'&&s[r+3][i]!='')
18             {
19                 if(!isspace(s[r+3][i])) dfs(r+3,i); //fgets读入的'
'也满足isspace
20                 i++;
21             }
22     }
23     printf(")");
24 }
25 void solve()
26 {
27     n=0;
28     for(;;)
29     {
30         fgets(s[n],maxn,stdin); // 学一下这个函数,读入maxn-1个字符,或者读到换行符停止
31         if(s[n][0]=='#') break;
32         else n++;
33     }
34     printf("(");
35     if(n)
36     {
37         for(int i=0;i<strlen(s[0]);i++)
38         if(s[0][i]!=' ') {dfs(0,i);break;}
39     }
40     printf(")
");
41 }
42 int main()
43 {
44     int t;
45     fgets(s[0],maxn,stdin);
46     sscanf(s[0],"%d",&t);
47     while(t--)
48     {
49         solve();
50     }
51     return 0;
52 }
原文地址:https://www.cnblogs.com/yijiull/p/6777771.html