1. 程式人生 > >UVA10562-Undraw the Trees(遞歸)

UVA10562-Undraw the Trees(遞歸)

long com \n limit 簡單 work recent oid code

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語言學的時候不太註意的一個點,就是‘\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 }

UVA10562-Undraw the Trees(遞歸)