您的位置:首页 > 其它

Undraw the Trees

2015-08-21 10:34 162 查看
Undraw the Trees

大意:

由一张图求出该树的先序遍历;

要点:

节点的字符可能是任意的ASCII字符但不包括'|','-' ,' ';

可以由|知道该节点是否有左右分支;

代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
using namespace std;

string str[205];

void bfs(int beg, int end, int cur){
cout << "(";
for (int i = beg; i < end && i < str[cur].length(); i++){
if (str[cur][i] != ' ' && str[cur][i] != '#'){
cout << str[cur][i];
if (str[cur + 1][i] == '|'){
int b, e;
for (b = i; b >= 0 && str[cur + 2][b] == '-'; b--){}
for (e = i; e < str[cur + 2].length() && str[cur + 2][e] == '-'; e++){}
bfs(b + 1, e, cur + 3);
}
else
cout << "()";
}
}
cout << ")";
}

int main(){
int num;
cin >> num;
getchar();
while (num--){
int x;
for (x = 0; getline(cin, str[x]) && str[x] != "#"; x++){}
bfs(0, str[0].length(), 0);
cout << endl;
for (int i = 0; i < 205; i++)
str[i] = "";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: