UVaOJ10562---Undraw the Trees
来源:互联网 发布:房地产行业 黑心 知乎 编辑:程序博客网 时间:2024/06/05 09:15
10562 - Undraw the Trees
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.
Sample Professor’s Trees Corresponding Our Trees
2
A
|
--------
B C D
| |
----- -
E F G
#
e
|
----
f g
#
(A(B()C(E()F())D(G())))
(e(f()g()))
Problemsetter: Monirul Hasan, Member of Elite Problemsetters' Panel
#include <iostream>#include <cstdio>#include <cstring>#include <cctype>using namespace std;char disk[205][205];char ourTree[200000];int line[205];int r;inline bool IsNode(char c){ return isprint(c) && c != '|' && c != '-' && c != ' ' && c != '#';}void ConvertTree(int row, int col, int &idx){ ourTree[idx++] = disk[row][col]; ourTree[idx++] = '('; if(row + 1 < r && col < line[row + 1] && disk[row + 1][col] == '|') { int rr = row + 2; int left = col, right = col; for( ; left > 0 && disk[rr][left - 1] == '-'; --left); for( ; right < line[rr] - 1 && disk[rr][right + 1] == '-'; ++right); for(int i = left; i <= right; ++i) { if(i < line[rr + 1] && IsNode(disk[rr + 1][i])) ConvertTree(rr + 1, i, idx); } } ourTree[idx++] = ')';}int main(){ int ca; scanf("%d", &ca); getchar(); while(ca--) { r = 0; memset(disk, 0, sizeof(disk)); //注意每轮开始时清空数组。如果去掉该句,会wa. while(gets(disk[r]) && disk[r][0] != '#') { line[r] = strlen(disk[r]); ++r; } int idx = 0; ourTree[idx++] = '('; for(int i = 0; i < line[0]; ++i) { if(IsNode(disk[0][i])) ConvertTree(0, i, idx); } ourTree[idx++] = ')'; ourTree[idx++] = '\0'; printf("%s\n", ourTree); } return 0;}
- UVaOJ10562---Undraw the Trees
- uvaoj10562 Undraw the Trees(一直WA的快来看看)
- 10562 - Undraw the Trees***
- 10562 - Undraw the Trees
- UVA10562 - Undraw the Trees
- UVA10562- Undraw the Trees
- Undraw the Trees UVA10562
- 10562 - Undraw the Trees
- 10562 - Undraw the Trees
- uva10562 Undraw the Trees
- 10562 - Undraw the Trees
- Undraw the Trees
- uva10625 Undraw the Trees
- uva10562 Undraw the Trees
- 10562 - Undraw the Trees
- uva10562 -- Undraw the Trees
- Undraw the Trees UVA
- uva 10562 - Undraw the Trees
- 在共享DLL中使用MFC 和在静态库中使用MFC的区别
- android--BroadCastReceiver 简介
- svn中导出来的项目运行出错
- php遍历目录下的文件夹
- android基础学习(7)-------spinner的使用
- UVaOJ10562---Undraw the Trees
- php冒泡排序算法
- csdn今天改版界面了。 记录下
- O(n)复杂度,求数组中第2大的数
- $.ajax返回的JSON无法执行success的解决方法
- Hadoop是怎么分块的
- oozie rerun job in 2.3.0-cdh3u0
- vs2008\drivers\opengl.h.c_opengl显示键盘鼠标等=模拟LCD终极模板
- 二维数组定义以及动态分配空间