UVA10562

题目的意思就是给你一棵树,直接画出来那种,如果不是‘|’,空格,‘-’ ‘\n’ .'#'就是节点;
一开始一心想建树,然后读入也一直读不清楚,主要是不知道怎么把垂直关系弄出来,那个有子树啥的。
无奈去看了题解,才发现,用二维数组存简单了很多,判断有没有子书,只要第一个下标加一就行了。。
半看半写的A了;

#include <iostream>
#include <cstring>

using namespace std ;

const int maxn = 210 ;

char str[maxn][maxn] ;
char node[maxn] ;
int rcount ;
int ncount ;
void init() {
    rcount = 0 ; ncount = 0 ;
    memset( str , 0 , sizeof( str ) ) ;
    memset( node , 0 , sizeof( node ) ) ;
    return ;
}
bool islable( char c ) {
    if( c == ' ' || c  == '|' || c == '#' || c == '-' || c == '/n' )
    return false ;
    return true ;
}
void dfs( char s[][maxn] , int b , int e , int r ) {
    node[ncount++] = '(' ;
    for( int i = b ; i <= e && i < strlen( s[r] ) ; i++ )
    if( islable( s[r][i] ) )  {
        if( s[r+1][i] == '|' )  {
            node[ncount++] = s[r][i] ;
            int nb = -1 ; int ne = strlen( s[r+2] ) - 1 ;
            for( int j = i ; j >= 0 ; j-- )
            if( s[r+2][j] != '-' )
            { nb = j ; break ; }
            for( int j = i ; j < strlen( s[r+2] ) ; j++ )
            if( s[r+2][j] != '-' )
            { ne = j ; break ; }
            if( nb == -1 ) nb = 0 ;
            dfs( s , nb , ne , r + 3 ) ;
        }
        else if( islable( s[r][i] ) ) {
            node[ncount++] = str[r][i] ;
            node[ncount++] = '(' ; node[ncount++] = ')' ;
        }

    }
    node[ncount++] = ')' ;
    return ;
}

int main() {
    int ncase ;
    cin >> ncase ;
    getchar() ;
    while( ncase-- )  {
        init() ;
        while( 1  ){
            gets( str[rcount] ) ;
            if( str[rcount++][0] == '#' ) break ;
        }
        dfs( str , 0 , strlen( str[0] ) - 1 ,0 ) ;
        cout << node << endl ;
    }
    return 0 ;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值