UVA - 10562 Undraw the Trees_professor homer has been reported missing. we susp-程序员宅基地

技术标签: uva  二叉树  UVA - 10562 Undraw t  

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 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()))

 




#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#define N 300
using namespace std;

char a[N][N];
int n, flag;

void dfs(int  h, int l, int x) {
	if (h > flag)
		return ;
	for (int i = l; i <= x; i ++) {
		if (i < strlen(a[h]) && a[h][i] != ' ' && a[h][i] != '|' && a[h][i] != '#' && a[h][i] != '-') {
			printf("%c(",a[h][i]);
			if (a[h + 1][i] == '|') {
				int m1,m2;
				m1 = m2 = i;
				while (a[h + 2][m1 - 1] == '-')
					m1--;
				while( a[h + 2][m2 + 1] == '-')
					m2 ++;
				dfs(h + 3, m1, m2);
			}
			printf(")");
		}	
	}
}

int main() {
	string str;
	cin >>n;
	getchar();
	while (n--) {
		flag = 0;
		memset(a, 0, sizeof(a));
		while (getline(cin, str)) {
			if (str[0] == '#')
			break;
			for (int i = 0; i < str.length(); i++) {
				a[flag][i] = str[i];	
			}
			++flag;

		}
	printf("(");
	dfs(0, 0, strlen (a[0]));
	printf(")\n");
	}
return 0;
}







版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zz_FreeSky/article/details/38750747

智能推荐

vue中input输入框限制输入小数点后1位_vue el-input 动态设置小数点后一位-程序员宅基地

文章浏览阅读822次。vue中input输入框限制输入小数点后1位:<input @input="InputChange" v-model="clllci" /> InputChange(e) { console.log(e.target.value.match(/^\d*(\.?\d{0,2})/g)[0],6666) this.clllci = e.target.value.match(/^\d*(\.?\d{0,2})/g)[0] || null; },..._vue el-input 动态设置小数点后一位

gopdf的基本使用_gopdf 换行-程序员宅基地

文章浏览阅读364次。pdf.Cell(nil, "早上好,文艺范")使用循环让文本自动换行。//绘制带圆角的矩形。_gopdf 换行

Android实现抖音无水印视频,最新大厂Android社招面试经验汇总-程序员宅基地

文章浏览阅读920次,点赞12次,收藏14次。最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2019-2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。还有高级架构技术进阶脑图、Android开发面试专题资料帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

ubuntu 18.04 python3安装 版本更换 指定python版本运行脚本 anaconda3冲突_把/usr/bin/python和/usr/bin/python3链接到anaconda/bin-程序员宅基地

文章浏览阅读1.7k次。一、python3安装sudo apt install python3二、版本更换1查询已安装的python版本ls /usr/bin/python*显示在update-alternatives中有的python版本sudo update-alternatives --list python添加python版本到update-alternatives中,后面的数字越高优先级越高..._把/usr/bin/python和/usr/bin/python3链接到anaconda/bin

一起学ORBSLAM2(5)ORBSLAM的单目视觉处理方式_orbslam的单目尺度-程序员宅基地

文章浏览阅读6.4k次,点赞2次,收藏38次。单目相机由于深度是未知的,因此我们需要对其进行初始化,在ORB-SLAM中将其用单独的类来表示,并将它写成单独的文件initializer.cc,注意单目相机即使在进行初始化之后,仍然存在尺度问题,初始化将第一帧的位移作为单位长度,后面的深度和位移都是依据这一标准进行的,所以尺度问题是单目slam的理论缺点。单目slam初始化需要两帧进行,第一帧作为参考初始化帧,第二帧作为当前帧。在第一帧来临时建..._orbslam的单目尺度

IK-Analyzer的maven项目依赖_ikanalyzer依赖-程序员宅基地

文章浏览阅读3.5k次。Java中文分词包——IK-Analyzer的maven项目依赖 <dependency> <groupId>com.janeluo</groupId> <artifactId>ikanalyzer</artifactId> <version>2012_u6</version&..._ikanalyzer依赖

随便推点

H3C的R390X G2服务器配置HCI时,关于磁盘配置jbod模式_华三服务器开启硬盘直通-程序员宅基地

文章浏览阅读5.9k次。前言 配置深信服超融合时,用到的是第三方服务器,一般情况下网络构架搭建好和设备齐全后,经常会遇到问题是:HCI平台配置虚拟存储时如果第三方服务器有RAID,建议配置为直通(JBOD)模式,如果RAID卡不支持该模式,则要求将每个硬盘做成RAID 0逻辑盘,同时建议关闭RAID卡的写缓存,此时不支持硬盘热拔插!(但是不同的第三方服务器的jbod配置不一样!)H3C的R390X G2..._华三服务器开启硬盘直通

VisualStudio2022 关于C4996和C6031错误的解决方法_vs2022 c4996-程序员宅基地

文章浏览阅读1.1k次,点赞6次,收藏9次。微软不建议再使用C的传统库函数scanf,strcpy,sprintf等,所以直接使用这些库函数会提示C4996错误。_vs2022 c4996

JSP基本使用_jsp 用法-程序员宅基地

文章浏览阅读256次。本文是个人看视频总结,适合初学者标记含义说明性的标记 通常会放在文件的顶部包含普通的Java代码,_jspService方法外部包含普通的Java代码, _jspService方法内部包含普通的Java代码,通常是用来赋值和展示jsp文件中注释内容JSP编译:设计一个登录页面查询展示金额的例子部分代码:文件名称:showBal_jsp 用法

Glide介绍及基本使用方法_glide使用-程序员宅基地

文章浏览阅读4k次,点赞6次,收藏34次。本文为Glide的基本使用技巧,基于Glide官方文档编写,适合未接触Glide的开发人员参考。_glide使用

c++ ifstream 读文件 最后一行读两次-程序员宅基地

文章浏览阅读908次。http://blog.csdn.net/rebel_321/article/details/4927464 用ifstream的eof(),竟然读到文件最后了,判断eof还为false。网上查找资料后,终于解决这个问题。参照文件:http://tuhao.blogbus.com/logs/21306687.html 在使用C/C++读文件的时候,一定都使用过eof()这个..._ifstream 读到了文件末尾以外的数据

什么是入侵防御系统(IPS)?底层原理是什么?_为啥需要ips入侵设备呢-程序员宅基地

文章浏览阅读1.7k次,点赞2次,收藏16次。攻击检测:IPS利用预定义的规则或策略来检测可能的网络攻击,例如网络扫描、DDoS攻击、SQL注入攻击、漏洞利用等。日志记录和分析:IPS可以记录和分析网络流量和事件日志,以帮助安全团队识别网络攻击和异常行为,并提供相关的安全报告和警报。综上所述,IPS通过流量监控、攻击检测、攻击阻止、漏洞管理和日志记录和分析等多种手段,保护网络安全并减少网络攻击的影响。攻击阻止:当IPS检测到可能的攻击时,它会采取相应的措施来阻止攻击,例如丢弃攻击数据包、阻止攻击源地址等。_为啥需要ips入侵设备呢

推荐文章

热门文章

相关标签