uva 10562

原题

一道蛮有意思的字符串处理题

建树什么的还是用递归比较方便

题目描述和思路也都比较清晰, 就不再赘述了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN = 201;
const int BASE = 10;
typedef long long LL;
/*
uva 10562

		
*/

char input[MAXN][MAXN];
int row, col;

void GetScope(int r,int c,int &left,int &right){
	left = right = c;
	while( input[r][left]=='-' ){
		left--;
	}
	left++;
	while( input[r][right]=='-' ){
		right++;
	}
	return ;
}

bool ischar(char c){
	return ( c!='-' && c!='|' && c!=' ' && c!='#' && c!='\0' );
}

void visit(int r, int from, int to){
	for(int i=from; i<to; i++){
		if( ischar(input[r][i]) ){
			printf("%c(",input[r][i]);
			if( input[r+1][i]=='|' ){		// 有子树 
				int left,right;
				GetScope(r+2, i, left, right);
				visit(r+3, left, right);
			}
			printf(")");
		} 
	}
	return ;
}

int main(){
//	freopen("input2.txt","r",stdin);
	int T;
	cin >> T;
	while( T-- ){
		memset(input,0,sizeof(input));
		int i = 0, j = 0;
		char ch;
		do{
			j = 0;
			while( (ch=getchar())!='\n' && ch!=EOF ){
				input[i][j++] = ch;
			}
			if( strlen(input[i])>0 )	i++;
		}while( strcmp(input[i-1],"#")!=0 );
		row = i-1;
		col = strlen(input[0]);
		printf("(");
		visit(0,0,col);
		printf(")");
		cout << endl;
	}


	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值