理解后写了一遍,感觉不难。
#include<stdio.h>
#include<iostream>
#include<cctype>;
#include<string.h>
using namespace std;
const int maxn = 200 +10;
int n;
char buf[maxn][maxn];
void dfs(int r,int c){
printf("%c(",buf[r][c]);
if(r+1<n && buf[r+1][c]=='|'){
int x=c;
while(x-1>=0 && buf[r+2][x-1]=='-') x--;
while(buf[r+2][x]=='-' && buf[r+3][x]!='\0'){
if(!isspace(buf[r+3][x])) dfs(r+3,x);
x++;
}
}
printf(")");
}
void solve(){
memset(buf,0,sizeof(buf));
n=0;
for(;;){
fgets(buf[n],maxn,stdin);
//gets(buf[n]);
if(buf[n][0]=='#') break;
else n++;
}
printf("(");
if(n){
int i;
for(i=0;i<strlen(buf[0]);i++)
if(buf[0][i]!=' ') break;
dfs(0,i);
}
printf(")\n");
}
int main(){
int T;
scanf("%d%*c",&T);
while(T--) solve();
return 0;
}