UVA10562-Undraw the Trees(递归)

Problem UVA10562-Undraw the Trees

Accept: 1199  Submit: 8397

Time Limit: 3000 mSec

Problem Description

 

Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don’t know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor’s works were found in a 3.5” floppy disk left inside the drive. The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek —we leave this trivial task for you. Convert professor’s trees to our trees.

 

 Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.

 Output

The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 ≤ T ≤ 20) drawn in the file. Then you would have T trees, each ending with a single hash (‘#’) mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols ‘-’, ‘|’, ‘ ’ (space) and ‘#’. Every node that has children has a ‘|’ symbol drawn just below itself. And in the next line there would be a series of ‘-’ marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.

 

 Sample Input

2
#
    A
    |
--------
B  C   D
   |   |
 ----- -
 E   F G
#
e
|
----
f g
#

 

Sample output

(A(B()C(E()F())D(G())))

(e(f()g()))

 

题解:记录下来这个题主要是为了记录一个C语言学的时候不太注意的一个点,就是'\0'在isspace下是false,因此如果不在'\0'处break,就会出现一些神奇的错误。

别的就是一些简单的递归的东西,不赘述。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 using namespace std;
 7 
 8 const int maxn = 200+10;
 9 string gra[maxn];
10 int n;
11 
12 void dfs(int x,int y){
13     printf("%c(",gra[x][y]);
14     if(x+1<n && gra[x+1][y]=='|'){
15         int yl = y,yr = y;
16         while(yl-1>=0 && gra[x+2][yl-1]=='-') yl--;
17         while(yr+1<gra[x+2].size() && gra[x+2][yr+1]=='-') yr++;
18         for(int j = yl;j <= yr;j++){
19             if(gra[x+3][j] == '\0') break;
20             if(!isspace(gra[x+3][j])) dfs(x+3,j);
21         }
22     }
23     printf(")");
24 }
25 
26 void solve(){
27     printf("(");
28     if(n){
29         for(int j = 0;j < (int)gra[0].size();j++){
30             if(!isspace(gra[0][j])){
31                 dfs(0,j);
32                 break;
33             }
34         }
35     }
36     printf(")\n");
37 }
38 
39 int main()
40 {
41     //freopen("input.txt","r",stdin);
42     //freopen("output.txt","w",stdout);
43     int iCase;
44     scanf("%d",&iCase);
45     getchar();
46     while(iCase--){
47         for(int i = 0;i < maxn;i++) gra[i].clear();
48         n = 0;
49         while(getline(cin,gra[n++]) && gra[n-1][0] != '#'){};
50         n--;
51         solve();
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/npugen/p/9513723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值