UVA - 10562 Undraw the Trees
来源:互联网 发布:js页面刷新计数器 编辑:程序博客网 时间:2024/06/05 00:27
题目链接:http://vjudge.net/problem/UVA-10562
题意:将一棵多叉树转化成题目所要求的括号表示法。
分析:不用建树,将整棵树放在二维数组里一点点读取节点即可。题目不难,就是不太好解释。
#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<set>#include<map>#include<vector>#include<list>#include<deque>#include<queue>#include<cmath>using namespace std;const int maxn = 210;char s[maxn][maxn];char ans[10000];int cur,d;void dfs(int deep, int left, int right){ if (deep > d) return; for(int i = left; i <= right; i++) { if((i<strlen(s[deep])) && s[deep][i] != '|' && s[deep][i] != '-' && s[deep][i] != ' ' && s[deep][i] != '#') { ans[cur++] = s[deep][i]; ans[cur++] = '('; if(s[deep + 1][i] == '|') { int l = i, r = i; while(s[deep + 2][l] == '-') l--; while(s[deep + 2][r] == '-') r++; dfs(deep + 3, l + 1, r - 1); ans[cur++] = ')'; } else ans[cur++] = ')'; //cout << "**" << s[deep][i] << " ** " << i << endl; } } return;}int main(){ int t; scanf("%d",&t); getchar(); while(t--) { d = 0; while(gets(s[d]))//读取树存放在二维数组里 { if(s[d][0] == '#') break; d++; } cur = 0; ans[cur++] = '('; dfs(0, 0, strlen(s[0]) - 1); ans[cur++] = ')'; ans[cur] = 0; printf("%s\n",ans); } return 0;}/*2 A |--------B C D | | ----- - E F G#e|----f g#*/
0 0
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- uva-10562Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA - 10562 Undraw the Trees
- UVa 10562 - Undraw the Trees
- Uva - 10562 - Undraw the Trees
- uva 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- 如何提问?
- MyEclipse/Eclipse导入sun.misc.BASE64Encoder jar包步骤
- 直面坑爹的前端面试
- 斯坦福CS231n 课程学习笔记--线性分类器(Assignment1代码实现)
- CountDownLatch
- UVA - 10562 Undraw the Trees
- GitHub使用记录[1]
- 【Qzone】把喜欢的事做到极致 Kernel->JNI->Application
- PackageInstaller源码分析(一)
- Java-NowCoder-密码验证合格程序
- DirectX学习笔记(十三):取景变换矩阵计算及3D世界摄像机的原理分析和实现
- XXX-笔试最大连续子矩阵和
- 拉勾网职位数据爬取与分析(一)
- 小米4手机短信写入