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 ...
随机推荐
- [SCOI2012] 喵星球上的点名
Description 给定 \(N\) 个姓名串和 \(M\) 个点名串.询问每个点名串点到了多少姓名和每个姓名串被点到了几次.\(N\leq 5\cdot 10^4,M\leq 10^5\). S ...
- Python 的几种推导式
推导式 comprehensions(又称解析式):是 Python 中很强大的.很受欢迎的特性,具有语言简洁,速度快等优点.推导式包括: 1. 列表推导式 2. 字典推导式 3. 集合推导式 对以上 ...
- MyBatis(国税)
一.MyBatis概要 1.1.ORM介绍 对象关系映射(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),用于实现面向对象编程语言里不同类型系统 ...
- Extjs 项目中常用的小技巧,也许你用得着(3)
几天没写了,接着继续, 1.怎么获取表单是否验证通过: form.isValid()//通过验证为true 2.怎样隐藏列,并可勾选: hidden: true, 如果是动态隐藏的话: grid.ge ...
- oracle 外连接以及用on和where 的区别
Oracle 外连接(OUTER JOIN)包括以下: 左外连接(左边的表不加限制) 右外连接(右边的表不加限制) 全外连接(左右两表都不加限制) 对应SQL:LEFT/RIGHT/FULL OUT ...
- c# 检测是否有Sql非法字符
/// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字 ...
- 以前没有写笔记的习惯,现在慢慢的发现及时总结是多么的重要。 这一篇文章主要关于java多线程一些常见的疑惑点。因为讲解多线程的书籍和文章已经很多了,所以我也不好意思多说,嘻嘻嘻、大家可以去参考一些那些书籍。我这个文章主要关于实际的一些问题。同时也算是我以后复习的资料吧,。还请大家多多指教。 同时希望多结交一些技术上的朋友。谢谢。
在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 以下就是我们常见的问题了: 1. 为什么我们不能直接调用run()方法呢? 我的理解是:线程的运行 ...
- Python3 系列之 环境配置篇
以下所有操作是基于 Windows10 和 Python3.6 来进行的,其它平台和 python 版本请自行百度. 高效使用 Visual Studio Code 系列 环境安装 1.Python ...
- spark_wordcount
spark是基于scala写的,虽然spark有java API,或者python API,但是scala算是正统吧. 而且scala的语法书写起来十分的爽,不想java那样笨重,python不太了解 ...
- 2017-12-02 编程语言试验之Antlr4+JavaScript实现"圈4"
参考: ANTLR4: Making a compiler with the JavaScript runtime 演示效果虽弱, 还是先上图吧: 在线演示: 地址. 源码库: program-in- ...