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

分析: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. codeblocks与MINGW的配置

    最好直接下载带GW的Codeblocks,然后配置编译器,调试器,有几个地方要注意: 1 在setting->debugger下要搜到gdb.exe/gdb32.exe 2在debug-acti ...

  2. C#的 IComparable 和 IComparer接口及ComparableTo方法的 区别(非常重要)

    (1)https://blog.csdn.net/ios99999/article/details/77800819 C# IComparable 和 IComparer 区别 (2)https:// ...

  3. 在Linux上用Apache搭建Git服务器

    在Linux上用Apache搭建Git服务器   最近在学Linux,终于在Linux上用Apache搭建起了Git服务器,在此记录一下. 服务器:阿里云服务器 Linux版本:CentOS 6.5 ...

  4. Python自动化运维的职业发展道路(暂定)

    Python职业发展之路 Python自动化运维工程 Python基础 Linux Shell Fabric Ansible Playbook Zabbix Saltstack Puppet Dock ...

  5. 一个Java的小问题

    老师今天在讨论群里抛出了一个问题,让大家尝试思考一下他所给的一段代码输出是什么. 其代码如下: class T { void foo() { this.bar(); } void bar() { Sy ...

  6. 关于AJAX跨域和原生AJAX CORS跨域解决

    项目需求要在别人的域名下调用自己的接口,因为浏览器的同源策略是不允许不同域名下之间的信息交换,那就意味着要跨域处理 参考博客 :https://blog.csdn.net/Ulricalin/arti ...

  7. android 动态壁纸开发

    转:http://www.eoeandroid.com/thread-100389-1-1.html android 动态壁纸开发参考:http://www.ophonesdn.com/article ...

  8. Unity内置shader 下载

    Unity内置shader  4.3.1 版本的  其他版本可以自己修改名称 下载地址 http://download.unity3d.com/download_unity/builtin_shade ...

  9. Lesson 1 Finding fossil man

    Why are legends handed down by storytellers useful? We can read of things that happend 5000 years ag ...

  10. mysql 两表索引优化

    建表语句 CREATE TABLE IF NOT EXISTS `class`( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `card` INT(1 ...