UVa 10562 (特殊的输入处理方式) Undraw the Trees
题意:
给出一个二维字符数组,它代表了一棵树。然后将这棵树转化为括号表示法(以递归的形式)。
分析:
这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c++里面的cin.getline()
函数介绍:
从文件结构体指针stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中,每次最多读取bufsize-1个字符(第bufsize个字符赋'\0'),如果文件中的该行,不足bufsize个字符,则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行
学习一下对输入数据的细节处理。
- //#define LOCAL
- #include <cctype>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int maxn = + ;
- int n;
- char buf[maxn][maxn];
- void dfs(int r, int c)
- {
- printf("%c(", buf[r][c]);
- if(r+ < n && buf[r+][c] == '|')
- {
- int i = c;
- while(i- >= && buf[r+][i-] == '-') i--; //找"---"的左边界
- while(buf[r+][i] == '-' && buf[r+][i] != '\0') //‘\0’不满足isspace
- {
- if(!isspace(buf[r+][i])) dfs(r+, i); //换行符也同样满足isspace
- i++;
- }
- }
- printf(")");
- }
- void solve()
- {
- n = ;
- for(;;)
- {
- fgets(buf[n], maxn, stdin);
- if(buf[n][] == '#') break;
- else n++;
- }
- printf("(");
- if(n)
- {
- for(int i = ; i < strlen(buf[]); ++i)
- if(buf[][i] != ' ')
- {
- dfs(, i);
- break;
- }
- }
- printf(")\n");
- }
- int main(void)
- {
- #ifdef LOCAL
- freopen("10562in.txt", "r", stdin);
- #endif
- int T;
- fgets(buf[], maxn, stdin);
- sscanf(buf[], "%d", &T);
- while(T--) solve();
- return ;
- }
代码君
UVa 10562 (特殊的输入处理方式) Undraw the Trees的更多相关文章
- 禁止表单操作及JS控制输入的方式
<div>表单元素特殊属性<input type="text" value="禁止输入" disabled /></div> ...
- [DFS遍历图]UVA10562 Undraw the Trees
传送门: 1. UVA - 10562 2. Vjudge [看图写树] 将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...
- UVa10562 Undraw the Trees
注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...
- UVa 10562 Undraw the Trees 看图写树
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...
- 【例题 6-17 UVa 10562】Undraw the Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟+递归 [代码] #include <bits/stdc++.h> using namespace std; con ...
- 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 ...
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- 【紫书】Undraw the Trees UVA - 10562 递归,字符串
题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...
随机推荐
- 关于6410的sd卡和nandflash启动的区别
今天在公司我们队长问我个问题,关于cortex的sd启动流程和nandflash的启动流程,一下想不起来了,中午闲来无事就整理了整理当初6410的两种启动方式的区别.在这里写一下.有不对的请指点,我对 ...
- 一个inline-block的样式。
<span style="height: 31px; font-family: 宋体; font-weight: bold; color: rgb(160, 64, 0); text- ...
- MAC 13信道
房东的路由器一直连不上,手机却能连上,前天设置了13信道,后来又忘了,最后找到个连接WIFI的方法,在网络偏好设置里选择向导,诊断中可以连上wifi.
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- linux网卡速率和双工模式的配置
linux网卡速率和双工模式的配置 (2012-09-06 14:39:57) 转载▼ 标签: 科技 网络接口 协商 网卡 工具 it 分类: Linux 改变网络接口的速度和协商方式的工具miito ...
- What is the difference between database table and database view?
The database table has a physical existence in the database. A view is a virtual table, that is one ...
- HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- hdu 1269 迷宫城堡(强联通分量,基础)
这是一道模版题 题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include ...
- CAS(Compare and Swap)理解
什么叫CAS(Compare and Swap)? 硬件同步原语!! 什么蛋疼的名字,一般人很难理解.根据英文全称翻译==比较与交换,这个名字大致还能理解一点,目前先暂且这么理解吧. 有啥用处? 对 ...