UVA10562-Undraw the Trees(递归)

Posted npugen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 e?orts 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” ?oppy disk left inside the drive. The disk contained only one text ?le 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 ?rst line of the input ?le. 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 ?rst line of the input ?le (which you can assume comes from standard input) contains the number of trees, T (1 ≤ T ≤ 20) drawn in the ?le. 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语言学的时候不太注意的一个点,就是‘‘在isspace下是false,因此如果不在‘‘处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] == ) 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(")
");
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 }

 















以上是关于UVA10562-Undraw the Trees(递归)的主要内容,如果未能解决你的问题,请参考以下文章

Undraw the Trees UVA - 10562

uva 122 trees on the level——yhx

Uva122 Trees on the level

UVA-122(Trees on the level)

Uva 122 Trees on the level

Undraw the Trees (Uva 10562)