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.
Input
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.
Output
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.
Sample Input
2
A
|
--------
B C D
| |
----- -
E F G
#
e
|
----
f g
#
Sample Output
(A(B()C(E()F())D(G())))
(e(f()g()))
题意:
照着图建树,除了‘-’、‘|’、‘#’外都可以当节点,父子节点通过‘|’连接,‘-’的下一行对应位置为子节点,输出时每个节点后跟一个(),里面是子节点
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e2+7;
const ll mod=1e9+7;
inline ll read() {
ll x=0;
bool t=false;
char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
#define read read()
string s[205];
ll len;
void f(ll start,ll end, ll p){
for(int i=start;i<=end && i<s[p].length();i++){
if(s[p][i]==' '||s[p][i]=='#') continue;
cout<<s[p][i]<<"(";
if(p+1<len && s[p+1][i]=='|'){
int a,b;
a=b=i;
for( ;a-1>=0;a--){
if(s[p+2][a-1]!='-')
break;
}
for( ; ;b++){
if(s[p+2][b+1]!='-')
break;
}
f(a,b,p+3);
}
cout<<")";
}
}
int main() {
int n;
cin>>n;
getline(cin,s[len]);
while(n--){
len=0;
getline(cin,s[len]);
while(s[len].compare("#"))
getline(cin,s[++len]);
cout<<"(";
if(s[0].compare("#"))
// cout<<s[0].compare("#")<<endl;
f(0,s[0].length()-1,0);
cout<<")"<<endl;
}
return 0;
}