UVa 10562 Undraw the Trees(递归遍历)
题目链接:
https://cn.vjudge.net/problem/UVA-10562
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.
- /*
- 题意描述:
- 给出一棵多叉树,转化成括号表示法
- 解题思路:
- 将图存储再二维数组中,根据规则递归输出,无需建树
- 易错分析:
- 注意空树的输出
- */
- #include<bits/stdc++.h>
- const int maxn=+;
- int n;
- char buf[maxn][maxn];
- void dfs(int r,int c);
- int main()
- {
- int T;
- //freopen("testin.txt","r",stdin);
- scanf("%d",&T);
- getchar();//gets前吃掉换行符
- while(T--){
- memset(buf,,sizeof(maxn*maxn));
- n=;
- while(){
- gets(buf[n]);
- if(buf[n][] == '#')
- break;
- else
- n++;
- }
- /*for(int i=0;i<n;i++)
- puts(buf[i]);*/
- printf("(");
- if(n){//防止空树
- for(int i=;i<strlen(buf[]);i++){
- if(buf[][i] != ' '){
- dfs(,i);
- break;
- }
- }
- }
- printf(")\n");
- }
- return ;
- }
- //递归遍历并输出以buf[r][c]为根节点的树
- void dfs(int r,int c){
- printf("%c(",buf[r][c]);
- if(r+ < n && buf[r+][c] == '|'){//有子树
- int i=c;
- while(i- >= && buf[r+][i-] == '-') i--;
- while(buf[r+][i] == '-' && buf[r+][i] != '\0'){
- if(buf[r+][i] != ' ')
- dfs(r+,i);
- i++;
- }
- }
- printf(")");
- }
UVa 10562 Undraw the Trees(递归遍历)的更多相关文章
- UVa 10562 Undraw the Trees 看图写树
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...
- uva 10562 undraw the trees(烂题) ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...
- UVa 10562 Undraw the Trees
题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...
- UVA - 10562 Undraw the Trees(多叉树的dfs)
题意:将多叉树转化为括号表示法. 分析:gets读取,dfs就好了.注意,样例中一行的最后一个字母后是没有空格的. #pragma comment(linker, "/STACK:10240 ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
- [DFS遍历图]UVA10562 Undraw the Trees
传送门: 1. UVA - 10562 2. Vjudge [看图写树] 将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...
- UVa10562 Undraw the Trees
注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...
- (实用篇)PHP不用递归遍历目录下所有文件的代码
<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...
- 递归遍历XML节点属性和属性值
public static XmlDocument FileMergedIntoXML(string strXmlPathPublic) { string strXmlPathPublic = str ...
随机推荐
- 蒲公英: 一个提供App 存储、分发、Bug管理的网站
一.蒲公英内测应用, https://www.pgyer.com/ 内测应用,仅需两步: 将应用上传到网站,生成安装链接和二维码 用户在手机上打开安装链接,或扫码二维码,即可开始安装 二.蒲公英Bug ...
- 二、RHCSA试题解析
一.设置YUM仓库 YUM的软件库源地址为:http://content.example.com/rhel7.0/x86_64/dvd,将此配置为操作系统的默认软件仓库. 方法一(修改配置文件): v ...
- Windows 7 系统中开启 ASP.NET State Service 服务的方法
控制面板 -> 程序和功能 -> “打开或者关闭 Windows 功能”对话框 -> Internet 信息服务 -> 万维网服务 -> 应用程序开发功能 -> A ...
- SQLSERVER CXPACKET 等待
--SQLSERVER CXPACKET 等待 2013-6-11 2 --联机丛书: 3 --当尝试同步查询处理器交换迭代器时出现.如果针对该等待类型的争用成为问题时,可以考虑降低并行度 4 5 6 ...
- SecureCrt使用SSH2登陆海康相机
海康相机登陆SSH2: 1.安装SecureCRT2.登陆到相机,系统-安全管理-安全服务,勾选启用SSh3,打开SecureCRT,快速链接4.选择协议SSh2,主机Ip填入相机IP,端口默认22, ...
- WPF设计时
资料太少.中文没有.英文的也残缺不全.待补充.问题暂时解决. 设计器通过使用命名约定来发现自定义设计时程序集 运行时程序集与设计时程序集对应关系 加载顺序 程序集名称(*表示版本号,可省略) 0 ...
- MariaDB 用户与权限管理(12)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- 不同的最小割(cqoi2016,bzoj4519)(最小割树)
学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成 两个部分,如果结点\(s,t\)不在同一个部分中,则称这个划分是关于\(s,t\)的割.对于带权图来说,将 所有顶 ...
- [学习笔记]区间dp
区间 \(dp\) 1.[HAOI2008]玩具取名 \(f[l][r][W/I/N/G]\) 表示区间 \([l,r]\) 中能否压缩成 \(W/I/N/G\) \(Code\ Below:\) # ...
- MySQL order by的实现
1.使用索引的已有顺序 2.filesort算法 filesort算法的执行流程 filesort相关的参数 sort_buffer_size 算法排序缓冲区的大小,线程级缓存 max_l ...