UVA - 10562 Undraw the Trees(看图建树)
来源:互联网 发布:mac ruby 2.3.0 安装 编辑:程序博客网 时间:2024/06/01 08:25
Problem D
Undraw the Trees
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
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 withASCII 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 than200 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()))
题目大意:
你的任务是将多叉树转化为括号表示法。
题目解析:
直接在二维数组里面递归即可,无须建树。注意对空树的处理,以及节点标号可以打印任意字符。
#include <ctype.h>#include <stdio.h>#include <string.h>using namespace std;const int N = 300;char map[N][N];int n ;void dfs(int r,int c) {printf("%c(",map[r][c]);if( r+1 < n && map[r+1][c] == '|') {int i = c;while( i - 1 >=0 && map[r+2][i-1] == '-') { //找到--- 的左边界i--;}while(map[r+2][i] == '-' && map[r+3][i] != '\0') {if( !isspace(map[r+3][i]) )dfs(r+3,i);i++;}}printf(")");}void solve() {n = 0;memset(map,0,sizeof(map));for(int i = 0;; i++) {gets(map[i]);if(map[i][0] == '#')break;n++;}int len = strlen(map[0]);printf("(");if(n != 0) {for(int i = 0; i < len; i++) {if(map[0][i] != ' ') {dfs(0,i);break;}}}printf(")\n");}int main() {int t;while(scanf("%d",&t) != EOF) {getchar();while(t--) {solve();}}return 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 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
- Java 线程
- 实现Parcelable接口需要实现三个方法
- YUV422与RGB互相转
- ZOJ 1654 Place the Robots
- WIN7下PLSQL远程连接Oracle(不安装Oracle客户端的方式)
- UVA - 10562 Undraw the Trees(看图建树)
- Android:调用其他程序中的activity和Permission Denial: starting Intent 错误解决办法
- flash小球碰撞
- 微信公众平台 OAuth Access_Token获得
- Generics
- 改变占位符的颜色
- jquery 学习 一 初识jquery
- VMWare 10 安装 10.9 Mac OS 如何调整分辨率为 1920 * 1200
- 菜鸟入门:Java程序员学习之路