UVA10562 Undraw the Trees【树】

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 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.
Output
Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.
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()))
在这里插入图片描述

问题链接UVA10562 Undraw the Trees
问题简述:(略)
问题分析
    输入的树是字符图形,将其转换为表达式形式的树,是一种从文本到文本的处理。在字符界面时代,这种处理是常见的。
    用递归程序来处理是建树的一种好办法。递归过程就是树形的结构的。实际上不需要建树,只需要建立递归过程,然后输出层次结构即可。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA10562 Undraw the Trees */

#include <bits/stdc++.h>

using namespace std;

const int N = 200;
char s[N + 1][N + 1];
int lcnt;

void dfs(int x, int y)
{
    printf("%c(", s[x][y]);
    if(x < lcnt - 1 && s[x + 1][y] == '|') {        // 出现“|”有子树
        int i = y;
        while(i >= 1 && s[x + 2][i - 1] == '-') i--;  // 查找最左边' - '
        while(s[x + 2][i] == '-' && s[x + 3][i] != '\0') {
            if(s[x + 3][i] != ' ')    //查找子树根(相差3行)
                dfs(x + 3, i);
            i++;
        }
    }
    printf(")");
}

int main()
{
    int t;
    scanf("%d", &t);
    getchar();
    while(t--) {
        lcnt = 0;
        do {
            gets(s[lcnt++]);
        } while(strcmp(s[lcnt - 1], "#") != 0);
        lcnt--;

        printf("(");
        if(lcnt) {
            int len = strlen(s[0]);
            for(int i = 0; i < len; i++)        // 查找树根结点
                if(s[0][i] != ' ') {
                    dfs(0, i);
                    break;
                }
        }
        printf(")\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10457039.html