Uva10562

Posted zbtrs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Uva10562相关的知识,希望对你有一定的参考价值。

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.

Professor‘s Trees

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.

Our Trees

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 Professor’s Trees      Corresponding Our Trees

2

    A

    |

--------

B  C   D

   |   |

 ----- -

 E   F G

#

e

|

----

f g

#

 

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

(e(f()g()))

 

分析:这道题其实并不是很难,细节处理比较麻烦。

      首先可以发现这道题的输入时以“递归”形式给出的,那么我们也用递归来做,如果当前节点有子节点就往下递归,剩下的就是字符串处理的一些细节了.

当时在做这道题的时候,nl和nr全都初始化为i,事实上这样是不对的,因为如果找不到一个不等于-的字符,那么接下来的区间大小就会为1,答案是错误的,以后要细心了.

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>

using namespace std;

int T, top, cnt;
char s[1010][1010], ans[1010];

bool islable(char c)
{
    if (c ==   || c == | || c == # || c == - || c == /n)
        return false;
    return true;
}

void dfs(int l, int r, int depth)
{
    ans[++cnt] = (;
    for (int i = l; i <= r && i < strlen(s[depth]); i++)
        if (islable(s[depth][i]))
        {
        if (s[depth + 1][i] == |)
        {
            ans[++cnt] = s[depth][i];
            int nl = 0, nr = strlen(s[depth + 2]) - 1, j = i;
            for (int j = i; j >= 0; j--)
                if (s[depth + 2][j] != -)
                {
                nl = j;
                break;
                }
            for (int j = i; j <= strlen(s[depth + 2]) - 1; j++)
                if (s[depth + 2][j] != -)
                {
                nr = j;
                break;
                }
            dfs(nl, nr, depth + 3);
        }
        else
        {
            ans[++cnt] = s[depth][i];
            ans[++cnt] = (;
            ans[++cnt] = );
        }
        }
    ans[++cnt] = );
}

int main()
{
    cin >> T;
    getchar(); //过滤掉回车符 
    while (T--)
    {
        top = 0;
        cnt = 0;
        memset(ans, 0, sizeof(ans));
        memset(s, 0, sizeof(s));
        while (1)
        {
            gets(s[++top]);
            if (s[top][0] == #)
                break;
        }
        dfs(0, strlen(s[1]) - 1, 1);
        for (int i = 1; i <= cnt; i++)
            printf("%c", ans[i]);
        printf("\n");
    }

    return 0;
}

 

以上是关于Uva10562的主要内容,如果未能解决你的问题,请参考以下文章

Uva10562

6-17 看图写树 uva10562

UVa 10562 Undraw the Trees 看图写树

UVa 10562 看图写树

Undraw the Trees (Uva 10562)

UVa 1442 - Cave