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< ...
随机推荐
- eclipse导入项目上面有个红叉X
问题: 今天突然想到一个以前做过的项目,想导入到新环境中,发现不管咱整都一个红叉X, 我记得以前好像碰到过类似的问题,当时三秒搞定,谁知道时间一长,三分钟没有搞定. 还是记录下: 一般导入项目出错,肯 ...
- SVN提交失败:Changing file 'XXX' is forbidden by the server;Access to 'XXX' forbidden
解决方案:https://blog.csdn.net/m0_38084243/article/details/81503638 个人分析主要是后者,在SVN服务器上添加上我对本项目的读写权限即可: 添 ...
- ES6之新的数据结构
Set Set 类似于数组,是一种集合的数据结构,和 Array 之间最大的区别是: Set中所有的成员都是唯一的. 可以把Set想象成是一个: 既没有重复元素,也没有顺序概念的数组. Set 本身是 ...
- vim学习--usr03 moving around
Word movement 小写的"w""ge""e""b" w表示向后移动到一个单词开头 ge表示向前移动到一个单词末 ...
- 五、java基础-关键字this_static_super_abstract_final,finalize()方法finally语句块
1.关键字this 含义:this 是一个引用类型,代表当前对象,引用类型里面必然保存内存地址,在堆中的每个对象中存储,保存内存地址指向自身. 用法: 1)this可以用在成员方法中,里面保存内存地址 ...
- 十二 事务&安全问题&隔离级别
事务 transaction,指一组操作,里面包含许多单一逻辑,只要一个逻辑没有执行成功,那么都算失败.所有的数据都回到最初的状态(回滚). 为什么要有事务? 确保逻辑的成功,例子:银行转账 事务针对 ...
- thinkphp的增删改查命令 - (mysql-thinkphp) (4)
方法1,在namespace下面加2行 use think\Controller; use think\Db; 1.查询所有结果 $res = Db::query("select * fro ...
- Windows进程通信-共享内存空间
三个模块 1,game.exe,三个方法,控制台输入指令('A','B','R')分别控制三个方法的调用: 2,WGDll.dll,要注入到game进程中的dll文件: 3,myconsole.exe ...
- POJ 3292:Semi-prime H-numbers 筛选数
Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8216 Accepted: 3 ...
- 201705 Ruby基础拾遗
Mixin override 异常处理 super 与super() 使用%()处理需要string interpolation但同时也需要" "(double quote)的状况 ...