题意:将多叉树转化为括号表示法。

分析:gets读取,dfs就好了。注意,样例中一行的最后一个字母后是没有空格的。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char s[MAXN][MAXN];
int cnt;
void dfs(int x, int y){
printf("%c(", s[x][y]);
if(x == cnt){
return;
}
if(s[x + ][y] == '|'){//有子树
int st = y;
int et = y;
while(s[x + ][st - ] == '-' && st - >= ) --st;//找左边界,且要保证下标不为负数
while(s[x + ][et] == '-') ++et;//找右边界
for(int j = st; j < et && s[x + ][j] != '\0'; ++j){//横线的右边界下不一定有结点,所以s[x + 3][j] != '\0'
if(!isspace(s[x + ][j])){
dfs(x + , j);
printf(")");
}
}
}
}
int main(){
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
getchar();
while(T--){
memset(s, , sizeof s);
cnt = ;
while(gets(s[cnt])){//gets读入
if(s[cnt][] == '#') break;
++cnt;
}
printf("(");
int len = strlen(s[]);
for(int j = ; j < len; ++j){
if(cnt == ) break;//空树
if(!isspace(s[][j])){//是结点
dfs(, j);
printf(")");
}
}
printf(")\n");
}
return ;
}

UVA - 10562 Undraw the Trees(多叉树的dfs)的更多相关文章

  1. UVa 10562 Undraw the Trees(递归遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...

  2. UVa 10562 Undraw the Trees 看图写树

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

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

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  4. UVa 10562 Undraw the Trees

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

  5. UVa10562 Undraw the Trees

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

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

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

  7. UVa 10562 (特殊的输入处理方式) Undraw the Trees

    题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...

  8. 【紫书】Undraw the Trees UVA - 10562 递归,字符串

    题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...

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

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

随机推荐

  1. Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

    Springboot + redis + 注解 + 拦截器来实现接口幂等性校验   1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...

  2. angular 自定义服务封装自定义http请求

    在angular中将http请求,放置在一起封装成服务,可减少代码重复,方便使用 var ngpohttprest = angular.module('ngpohttprest', []); ngpo ...

  3. 5.1 Nginx的基本配置

    备注:worker_processes 1(数量建议跟系统CPU的核数相同,例如:2个CPU,每个CPU4核,建议为8),worker_connections 建议小于worker_rlimit_no ...

  4. shell脚本中 “set -e” 的作用

    #!/bin/bash set -e command 1command 2 每个脚本都应该在文件开头加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出.这样的好处是防止 ...

  5. python之常见模块(time,datetime,random,os,sys,json,pickle)

    目录 time 为什么要有time模块,time模块有什么用?(自己总结) 1. 记录某一项操作的时间 2. 让某一块代码逻辑延迟执行 时间的形式 时间戳形式 格式化时间 结构化时间 时间转化 总结: ...

  6. 十五 Spring的AOP的注解的通知类型,切入点的注解

    Spring的注解的AOP的通知类型 @Before:前置通知 @AfterReturning:后置通知 @Around:环绕通知 @AfterThrowing:异常抛出通知 @After:最终通知 ...

  7. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  8. Linux文件系统层次结构标准FHS

    文件系统层次结构标准(英语:Filesystem Hierarchy Standard,FHS)定义了Linux操作系统中的主要目录及目录内容.FHS由Linux基金会维护. 当前版本为3.0版,于2 ...

  9. 移动硬盘在MAC找不着了

    原因: 移动硬盘,还没有推出的时候,我就直接拔了,导致文件被损坏了. 在MAC系统下,试了很多命令,一不小心加载上了. 但是只读模式,此时我想应该是有损坏了,系统也提示要重新格式化,这个代价太大了,里 ...

  10. scrapy 实现mysql 数据保存

    开始用scrapy 爬取数据的时候  开始用同步操作始终会报1064  的错误  因为 mysql 语法和导入的字段不兼容 尝试了  n  次之后  开始用  异步爬取  虽然一路报错 但是还是能把数 ...