UVA10562Undraw the Trees

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1503


题意:将一棵树形式转化后输出,看样例即可理解。

题解:采用递归,对每个节点,输出其后再递归处理它所包含的子节点。通过“------”列来确定其子节点所在的范围。注意节点不一定为字母且可能存在空树。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-7;
const double INF=1e50;
const double pi=acos(-1);

#define N 15
char s[1000][300];
int n;

void print(int m,int w)
{
    printf("%c(",s[m][w]);

    if ((m+3>n) || s[m+1][w]!='|')//需要加上s[m+1][w]!='|',因为叶子节点不一定在树的最底端
    {
        printf(")");
        return ;
    }

    int p,q,l;
    l=strlen(s[m+2])-1;
    p=q=w;
    while (s[m+2][p-1]=='-' && p>0) p--;
    while (s[m+2][q+1]=='-' && q<l) q++;

    for (int i=p;i<=q && i<(int)strlen(s[m+3]);i++)
    if (s[m+3][i]!= '-' && s[m+3][i]!='|' && s[m+3][i]!=' ' && s[m+3][i]!='#')
    print(m+3,i);

    printf(")");
}

int main()
{
    freopen("a","r",stdin);

    int T,i;

    scanf("%d",&T);
    gets(s[0]);
    for (int kk=1;kk<=T;kk++)
    {
        n=0;
        while (strcmp(s[n],"#")!=0)
        {
            n++;
            gets(s[n]);
        }
        n--;

        printf("(");
        for (i=0;i<(int)strlen(s[1]);i++)
        if (s[1][i]!= '-' && s[1][i]!='|' && s[1][i]!=' ' && s[1][i]!='#')
        print(1,i);
        printf(")");

        printf("\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值