题目链接:
https://cn.vjudge.net/problem/UVA-10562
Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don't know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor's works were found in a 3.5" floppy disk left inside the drive.
The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek -- we leave this trivial task for you. Convert professor's trees to our trees.
Professor's Trees
The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 <= T <= 20)drawn in the file. Then you would have T trees, each ending with a single hash ('#') mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols '-', '|', ' ' (space) and '#'. Every node that has children has a '|' symbol drawn just below itself. And in the next line there would be a series of '-' marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.
Our Trees
Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.
/*
题意描述:
给出一棵多叉树,转化成括号表示法
解题思路:
将图存储再二维数组中,根据规则递归输出,无需建树
易错分析:
注意空树的输出
*/
#include<bits/stdc++.h> const int maxn=+;
int n;
char buf[maxn][maxn];
void dfs(int r,int c);
int main()
{
int T;
//freopen("testin.txt","r",stdin);
scanf("%d",&T);
getchar();//gets前吃掉换行符
while(T--){
memset(buf,,sizeof(maxn*maxn));
n=;
while(){
gets(buf[n]);
if(buf[n][] == '#')
break;
else
n++;
}
/*for(int i=0;i<n;i++)
puts(buf[i]);*/
printf("(");
if(n){//防止空树
for(int i=;i<strlen(buf[]);i++){
if(buf[][i] != ' '){
dfs(,i);
break;
}
}
}
printf(")\n");
}
return ;
}
//递归遍历并输出以buf[r][c]为根节点的树
void dfs(int r,int c){
printf("%c(",buf[r][c]);
if(r+ < n && buf[r+][c] == '|'){//有子树
int i=c;
while(i- >= && buf[r+][i-] == '-') i--;
while(buf[r+][i] == '-' && buf[r+][i] != '\0'){
if(buf[r+][i] != ' ')
dfs(r+,i);
i++;
}
}
printf(")");
}