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< ...
随机推荐
- Django问题 Did you rename .....a ForeignKey
给新加入的字段添加一个default默认值即可,让字段非空.然后在进行makemigrations,完成操作后删除相关默认值即可.
- SSIS 無法將保護的 XML 節點 "DTS:Password" 解密,錯誤為 0x8009000B "機碼用在特定狀態時無效
发现之前部署的SSIS,执行失败,查看日志 來源: 描述: 無法將保護的 XML 節點 -- ::-- ::-- :: DataReader 來源 [] 描述: System.Exception: S ...
- ABC155F - Perils in Parallel
简述题意 给你N个数对 表示坐标与状态(0/1), M个操作,给定一个区间,区间内的坐标的状态翻转 思路:看到区间修改,很容易想到差分,对数对sort,每个a_i与a_i-1异或构造差分数组b,每次对 ...
- 获取美拍视频的链接--JS分析
美拍链接:https://www.meipai.com/ 找到视频链接的标签,源代码中没有这个div 通过Fiddler抓包,找到class="mp-h5-player-layer-vide ...
- vh搭配vw进行响应式布局
1.浏览器兼容性: IE8-不支持,IOS7.1-不支持,android4.3-不支持 2. vh代表浏览器视口高度(100vh等于当前浏览器的整个高度) 3.vw代表浏览器视口的宽度 (100vw等 ...
- @Qualifier
当一个接口,有多个实现类且均已注入到spring容器中了,使用时@AutoWired是byType的,而这些实现类类型都相同,此时就需要使用@Qualifier明确指定使用那个实现类.因此,@Qual ...
- 02-02Android 学习进度报告二
今天我主要学习了Android的UI基础布局知识,主要是学习View与ViewGroup的概念以及其区别. 首先是代码说明: <?xml version="1.0" enco ...
- css 瀑布流
瀑布流 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 本地VMware虚拟机环境CentOS7.6 安装宝塔Linux面板
之前测试一直都在使用phpenv和phpstudy ,不过这两个集成环境时长容易出bug,各种问题劝退.之前Windows开始Linux的体验又不尽人意,今天介绍一个别的方法.VMware安装Linu ...
- iterm2常用快捷键
标签 新建标签: command + t 关闭标签: command + w 切换标签: command + 数字 command + 左右方向键 切换全屏: command + enter 查找: ...