一道蛮有意思的字符串处理题
建树什么的还是用递归比较方便
题目描述和思路也都比较清晰, 就不再赘述了
#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;
}