UVA10562-Undraw the Trees(递归)
Problem UVA10562-Undraw the Trees
Accept: 1199 Submit: 8397
Time Limit: 3000 mSec
Problem Description
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.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that indicates the number of plates (1 ≤ N ≤ 100000). Then exactly N lines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters ‘a’ through ‘z’ will appear in the word. The same word may appear several times in the list.
Output
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.
Sample Input
2
#
A
|
--------
B C D
| |
----- -
E F G
#
e
|
----
f g
#
Sample output
(A(B()C(E()F())D(G())))
(e(f()g()))
题解:记录下来这个题主要是为了记录一个C语言学的时候不太注意的一个点,就是'\0'在isspace下是false,因此如果不在'\0'处break,就会出现一些神奇的错误。
别的就是一些简单的递归的东西,不赘述。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std; const int maxn = +;
string gra[maxn];
int n; void dfs(int x,int y){
printf("%c(",gra[x][y]);
if(x+<n && gra[x+][y]=='|'){
int yl = y,yr = y;
while(yl->= && gra[x+][yl-]=='-') yl--;
while(yr+<gra[x+].size() && gra[x+][yr+]=='-') yr++;
for(int j = yl;j <= yr;j++){
if(gra[x+][j] == '\0') break;
if(!isspace(gra[x+][j])) dfs(x+,j);
}
}
printf(")");
} void solve(){
printf("(");
if(n){
for(int j = ;j < (int)gra[].size();j++){
if(!isspace(gra[][j])){
dfs(,j);
break;
}
}
}
printf(")\n");
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int iCase;
scanf("%d",&iCase);
getchar();
while(iCase--){
for(int i = ;i < maxn;i++) gra[i].clear();
n = ;
while(getline(cin,gra[n++]) && gra[n-][] != '#'){};
n--;
solve();
}
return ;
}
UVA10562-Undraw the Trees(递归)的更多相关文章
- UVa10562 Undraw the Trees
注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...
- [DFS遍历图]UVA10562 Undraw the Trees
传送门: 1. UVA - 10562 2. Vjudge [看图写树] 将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...
- Uva10562——Undraw the Trees
上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...
- uva 10562 undraw the trees(烂题) ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...
- UVa 10562 (特殊的输入处理方式) Undraw the Trees
题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...
- UVa 10562 Undraw the Trees 看图写树
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...
- 【例题 6-17 UVa 10562】Undraw the Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟+递归 [代码] #include <bits/stdc++.h> using namespace std; con ...
随机推荐
- #5 Python变量与输入输出
前言 学习一门编程语言,最基本的无非不过学习其变量规则.条件语句.循环语句和函数,接下来的几节将开始记录这些基本的语法,本节主要记录变量规则! 一.Python输入输出 在说Python变量之前,先补 ...
- HTML5 audio元素如何使用js与jquery控制其事件
前言: 每一次遇见问题想到的就是怎么解决?最好的方法还是查询网络媒体,更好的办法是让自己记忆,只有自己理解到了才真正是属于自己.要做一个订单提醒功能,没有使用audio相关的插件,虽然插件无数,还是喜 ...
- hexo自动部署到git、ftp(虚拟主机等)、云服务器的方式
自动部署很有用,当你写完文章后,直接使用hexo d就可以自动更新你的网站了 部署到git 首先你需要在你的blog下安装git deployer插件:npm install hexo-deploye ...
- [转]Angular2: Cannot read property 'name' of undefined
本文转自:https://stackoverflow.com/questions/39755336/angular2-cannot-read-property-name-of-undefined 处理 ...
- vue 前台传后台
var the = this:let url = "/api/Purchase_Enter/CancelEnter"; let params = { Enter_Id: Enter ...
- Socket 类
构造函数 名称 说明 Socket(AddressFamily, SocketType, ProtocolType) 新实例初始化 Socket 类使用指定的地址族. 套接字类型和协议. ...
- 【Java基础】9、Enumeration接口和Iterator接口的区别
package java.util; public interface Enumeration<E> { boolean hasMoreElements(); E next ...
- C#特性:ConditionalAttribute
ConditionalAttribute类 msdn解释: 指示编译器应忽略方法调用或属性,除非已定义指定的条件编译符号. 命名空间: System.Diagnostics 语法: // // 摘要: ...
- SVN多项目并行版本管理解决方案
1.背景 随着公司业务拓展,各业务部门频繁的需求变更,导致系统集成冲突的问题日益突出. 2.现状 基于SVN版本管理模式,多分支版本并行,分支合并主干交付.多分支开发存在依赖关系且有交付的先后顺序, ...
- html标签必备
常用快捷键 Ctrl+c 复制 Ctrl+v 粘贴 Ctrl+x 剪切 Ctrl+a 全选 Ctrl+s 保存 Ctrl+z 撤销一步 Windows+d 返回桌面 Windows+e 我的电脑 Wi ...