UVA - 10562 Undraw the Trees(多叉树的dfs)
题意:将多叉树转化为括号表示法。
分析: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)的更多相关文章
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- 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 ...
- 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(... ...
- UVa 10562 (特殊的输入处理方式) Undraw the Trees
题意: 给出一个二维字符数组,它代表了一棵树.然后将这棵树转化为括号表示法(以递归的形式). 分析: 这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c ...
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
随机推荐
- day19-Python运维开发基础(类的魔术方法)
1. __new__魔术方法 # ### __new__ 魔术方法 ''' 触发时机:实例化类生成对象的时候触发(触发时机在__init__之前) 功能:控制对象的创建过程 参数:至少一个cls接受当 ...
- HttpClient 以post的方式发送请求(由于请求参数太多所以改成以post提交表单的方式)
1:Util类方法 /** * 发送 Post请求 * * @param url * @param reqXml * @return */ public static String post(Stri ...
- 莫烦 - Pytorch学习笔记 [ 一 ]
1. Numpy VS Torch #相互转换 np_data = torch_data.numpy() torch_data = torch.from_numpy(np_data) #abs dat ...
- 设计模式课程 设计模式精讲 8-8 单例设计模式-Enum枚举单例、原理源码解析以及反编译实战
1 课堂解析 2 代码演练 2.1 枚举类单例解决序列化破坏demo 2.2 枚举类单例解决序列化破坏原理 2.3 枚举类单例解决反射攻击demo 2.4 枚举类单例解决反射攻击原理 3 jad的使用 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:表示需要谨慎操作的按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 新见Java数据类型_需了解
LinkedList<T>.LinkedList.poll() 先给出结论:pop 与 poll 都是取出 LinkedList 的第一个元素,并将该元素删除,等效于:removeFirs ...
- Hadoop基准测试(二)
Hadoop Examples 除了<Hadoop基准测试(一)>提到的测试,Hadoop还自带了一些例子,比如WordCount和TeraSort,这些例子在hadoop-example ...
- 伪类:after,:before的用法
:after和:before是css3中的伪类元素.用法是像元素的前或者后插入元素.以after为例: li:after{ content: ''; color: #ff0000; } 意思是向li元 ...
- 【C++】【STL】【map】基础知识干货
1.map简介 map是一种关联式容器,主要用于对数据一对一的映射. 2.map的构造 (1)头文件:#include<map> (2)定义:map<第一关键字,第二关键字> ...
- 写给java web一年左右工作经验的人
摘要 大学就开始学习web,磕磕绊绊一路走过来,当中得到过开源社区很多的帮助,总结了这些年来的技术积累,回馈给开源社区. ps:图片都是从网上盗...感谢原作者. ps:文字千真万确都是我自己写的 ...