题目链接:

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(递归遍历)的更多相关文章

  1. UVa 10562 Undraw the Trees 看图写树

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...

  2. uva 10562 undraw the trees(烂题) ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  3. UVa 10562 Undraw the Trees

    题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...

  4. UVA - 10562 Undraw the Trees(多叉树的dfs)

    题意:将多叉树转化为括号表示法. 分析:gets读取,dfs就好了.注意,样例中一行的最后一个字母后是没有空格的. #pragma comment(linker, "/STACK:10240 ...

  5. 看图写树 (Undraw the Trees UVA - 10562)

    题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...

  6. [DFS遍历图]UVA10562 Undraw the Trees

    传送门: 1. UVA - 10562 2. Vjudge [看图写树]     将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...

  7. UVa10562 Undraw the Trees

      注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...

  8. (实用篇)PHP不用递归遍历目录下所有文件的代码

    <?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...

  9. 递归遍历XML节点属性和属性值

    public static XmlDocument FileMergedIntoXML(string strXmlPathPublic) { string strXmlPathPublic = str ...

随机推荐

  1. FastReport套打 和连续打印

    FastReport套打,纸张是连续的带锯齿的已经印刷好的,类似于通信公司发票这里设计的是客户销售记录.客户有两个要求:1.因为打印纸张是印刷的,明细记录只有8行,所以,如果明细记录如果不到8行,就将 ...

  2. Azure DevOps Server(TFS 2019) 中的SonarQube扫描任务出现错误:AppTest.java can't be indexed twice

    SonarQube错误描述 将一个Maven示例程序导入到Azure DevOps的待库中,执行SonarQube扫描过程时, DevOps Server提示下面的错误信息: [ERROR] Fail ...

  3. HSmartWindowControl 之 摄像头实时显示( 使用 WPF )

    1.添加Halcon控件,创建WPF项目 在VS2013中创建一个WPF工程,然后添加halcon的控件和工具包,参见: HSmartWindowControl之安装篇 (Visual Studio ...

  4. net 把指定 URI 的资源下载到本地

    DirectoryInfo dir = new DirectoryInfo(AppContext.BaseDirectory); var path = dir.FullName + @"te ...

  5. ovs stp

    环路拓扑 组成拓扑结构的脚本 构成连通脚本 ip netns add ns1 ovs-vsctl add-br br1 ovs-vsctl add-port br1 tap1 -- set Inter ...

  6. PS插件CameraRaw-初次尝试

    一.百度百科原话 RAW的原意就是“未经加工”.可以理解为:RAW图像就是CMOS或者CCD图像感应器将捕捉到的光源信号转化为数字信号的原始数据.RAW文件是一种记录了数码相机传感器的原始信息,同时记 ...

  7. Flash 0day漏洞(CVE-2018-4878)复现

    该漏洞影响 Flash Player 当前最新版本28.0.0.137以及之前的所有版本,而Adobe公司计划在当地时间2月5日紧急发布更新来修复此漏洞. 本文作者:i春秋作家——F0rmat 前言 ...

  8. GoLang学习控制语句之字符串

    Go语言字符串是一种值类型,且值不可变,即创建某个文本后你无法再次修改这个文本的内容:更深入地讲,字符串是字节的定长数组.Go 代码使用 UTF-8 编码(且不能带 BOM),同时标识符支持 Unic ...

  9. 利用Knockoutjs对电话号码进行验证

    问题来源 最近在项目中前端使用Knockoutjs,验证模块自然也是使用Knockoutjs来进行表单验证了,比较头痛,因为没有使用过Knockoutjs,更加别说要去用它做表单验证了,于是乎恶补了一 ...

  10. String s=“dd”和String s=new String("dd")区别

    Java中String s="dd"的话会先检查常量池中是否有"dd"这个字符串,如果没有则创建一个,然后将s指向字符串的地址,而new String(&quo ...