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 ...
随机推荐
- SpringBoot入门之基于XML的Mybatis
上一博客介绍了下SpringBoot基于注解引入Mybatis,今天介绍基于XML引入Mybatis.还是在上一篇demo的基础上进行修改. 一.Maven引入 这个与上一篇的一样,需要引入mybat ...
- net4log 添加自定义变量
在log4net.config中 <parameter> <parameterName value="@czyid" /> <dbType value ...
- 《深入理解Java虚拟机》(四)虚拟机性能监控与故障处理工具
虚拟机性能监控与故障处理工具 详解 4.1 概述 本文参考的是周志明的 <深入理解Java虚拟机> 第四章 ,为了整理思路,简单记录一下,方便后期查阅. JDK本身提供了很多方便的JVM性 ...
- c#实战开发:用.net core开发一个简单的Web以太坊钱包 (六)
今天就来开发一个C# 版的简易钱包 先回顾以前的内容 c#实战开发:以太坊Geth 命令发布智能合约 (五) c#实战开发:以太坊Geth 常用命令 (四) c#实战开发:以太坊钱包快速同步区块和钱包 ...
- Centos7下开启80端口
firewall-cmd --zone=/tcp --permanent 命令含义: --zone #作用域 --add-port=80/tcp #添加端口,格式为:端口/通讯协议 -- ...
- java中自己常犯的错误汇总
package debug; /** 1.定义一个公共的动物类,包含名字.年龄.颜色和吃饭东西方法 2.定义一个猫类,继承动物类,同时拥有玩游戏的本领 3.定义一个狗类,继承动物类,同时拥有看门的本领 ...
- Matlab forward Euler demo
% forward Euler demo % take two steps in the solution of % dy/dt = y, y(0) = 1 % exact solution is y ...
- Python 练习:使用 # 号输出长方形
使用 # 号输出一个长方形,用户可以指定宽和高 height = int(input("please input height: "))width = int(input(&quo ...
- HBuilder开发ios App离线打包启动画面无效的解决方法
其中容易忽略的一点是manifest.json文件.plus下加入如下配置: "splashscreen": { "autoclose": false,/*如果 ...
- 洗礼灵魂,修炼python(56)--爬虫篇—知识补充—编码之url编码
其实在最前面的某一篇博文里,是绝对提过编码的,有ASCII,有UTF-8,有GB2312等等,这些我绝对说过的. url编码 首先,Http协议中参数的传输是"key=value" ...