UVA 10562 - Undraw the Trees(看图写树) By SuCicada

例题6-17 看图写树(Undraw the Trees, UVa 10562)
你的任务是将多叉树转化为括号表示法。如图6-16所示,每个结点用除了“-”、“|”和空格
的其他字符表示,每个非叶结点的正下方总会有一个“|”字符,然后下方是一排“-”字符,恰
好覆盖所有子结点的上方。单独的一行“#”为数据结束标记。

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


本家地址


先记录树,然后从头开始递归遍历树,并且构造先序遍历字串。
非叶子结点字母下方必有竖线。竖线下方必有若干横线,横线下方必有若干字母,即子节点。


#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;

char tree[222][222];


int isRightChar(char c){
    return c && c!='-' && c!='|' && c!=' ' && c!='#';
}

int loop(int x,int y){
    cout<<tree[x][y];
    cout<<"(";
    if(tree[x+1][y]!='|'){
        return 0;
    }else{
        int a;
        for(a=y;tree[x+2][a]=='-';a--){}
        a++;    
        int b;  
        for(b=y;tree[x+2][b]=='-';b++){}
        b--;

        for(int i=a;i<=b;i++){
            if(isRightChar(tree[x+3][i])){
                loop(x+3,i);
                cout<<")";
            }
        }
    }
    return 1;
}


int main(){
    int T;
    cin>>T;
    getchar();
    while(T--){
        memset(tree,0,sizeof(tree));
        int line=0;
        int row=0;
        while(1){
            string s;
            getline(cin,s);
            row=0;
            if(s.size()==1 && s[0]=='#'){
                break;
            }else{
                for(int i=0;i<s.size();i++){
                    tree[line][row++] = s[i];
                }
                line++;
            }
        }

        cout<<"(";
        for(int i=0;i<200;i++){
            if(isRightChar(tree[0][i])){
                loop(0,i);
                cout<<")";
                break;
            }
        }
        cout<<")"<<endl;


    }
    return 0;
}

// AC at 2020/08/16
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值