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;
}
大意:
由一张图求出该树的先序遍历;
要点:
节点的字符可能是任意的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;
}
相关文章推荐
- 如何进行用户行为分析
- iOS开发 -- 音频的播放
- su与sudo的区别与使用技巧
- 玩转Bootstrap(基础) (3.按钮)
- POJ 2763 -- Housewife Wind 树链剖分 LCT 动态树
- cross-site tracing XST攻击
- HDU 1792-A New Change Problem(互质数的最大不能表示数)
- Gephi—结合GeoLayout和Maps of countries layouts的地理位置布局
- ubuntu下 安装 adb
- php 操作 Memcache
- Storm入门教程(五):一致性事务
- css的#和.的区别
- MVC中在RAZOR 模板里突然了现了 CANNOT RESOLVE SYMBOL ‘VIEWBAG’ 的错误提示
- 剑指offer_快速查找递增二维数组中是否存在目标
- 13家中国域名商(国际域名)解析量报告(8月18日)
- 在VirtualBox虚拟机下安装linux小红帽
- 汇率战争
- Swift学习笔记(十七)协议
- rust raw pointer使用方法(1)
- HDU-1533 Going Home(二分图匹配)