uva-10562 Undraw the Trees

解决一个教授失踪案件中发现的问题,即将ASCII绘制的树转换为一种特定的括号表示法。此任务涉及读取标准输入中的ASCII树,并按照特定规则输出转换后的树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem D
Undraw the Trees 
Input: 
Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

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()))

 


Problemsetter: Monirul Hasan, Member of Elite Problemsetters' Panel


题目给你一颗特点形状的树,按照特定格式--(树根(子树))--将树的先序遍历输出,显然直接递归处理。

刚开始没考虑空树情况一直wa,补上空树直接输出()后ac

 

#include <iostream>
#include <string>

using namespace std;

string str[201];
int layer;

int isNode( char ch)  //判断是否为节点
{
    if( ch != '-' && ch != '|' && ch != ' ' )
        return 1;
    else return 0;
}

void convert( int m, int root)   //m表示root所在层数,root表示根的下标
{

    cout << str[m][root] << '(';   //输出当前树的根和‘('

    if( root < str[m+1].length() && str[m+1][root] == '|'){  //如果有儿子,先递归的处理子树

        int s = root, e = root;                          //找到儿子所在下标范围
        while( str[m+2][s-1] == '-' && s-1 >= 0) s--;
        while( str[m+2][e+1] == '-' && e+1 < str[m+2].length()) e++;

        for( int i = s; i <= e && i < str[m+3].length(); i++)
            if( isNode(str[m+3][i]))     //输出子树
                convert( m+3, i);

    }

    cout << ')';  


}

int main()
{
    int t;
    cin >> t;
    cin.ignore();
    while( t--){
        layer = 0;
        while( getline( cin, str[layer])) { if( str[layer][0] == '#') break; layer++;} //读入数据直到#

        cout << '(';
        if( str[0][0] != '#')  //若为空树直接跳过。
        for( int i = 0; i< str[0].length(); i++)
            if( isNode( str[0][i])){ //找到根,开始转化输出
               convert( 0, i);
                break;
            }
        cout << ')';
        cout << endl;
    }
    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值