题意:

给出一个二维字符数组,它代表了一棵树。然后将这棵树转化为括号表示法(以递归的形式)。

分析:

这道题最大的特色就是对数据的处理方式,里面用到了一个 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的更多相关文章

  1. 禁止表单操作及JS控制输入的方式

    <div>表单元素特殊属性<input type="text" value="禁止输入" disabled /></div> ...

  2. [DFS遍历图]UVA10562 Undraw the Trees

    传送门: 1. UVA - 10562 2. Vjudge [看图写树]     将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...

  3. UVa10562 Undraw the Trees

      注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...

  4. UVa 10562 Undraw the Trees 看图写树

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...

  5. 【例题 6-17 UVa 10562】Undraw the Trees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟+递归 [代码] #include <bits/stdc++.h> using namespace std; con ...

  6. uva 10562 undraw the trees(烂题) ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  7. UVa 10562 Undraw the Trees

    题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...

  8. UVa 10562 Undraw the Trees(递归遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...

  9. 【紫书】Undraw the Trees UVA - 10562 递归,字符串

    题意:给你画了一颗树,你要把它的前序输出. 题解:读进到二维数组.边解析边输出. 坑:少打了个-1. #define _CRT_SECURE_NO_WARNINGS #include<cstri ...

随机推荐

  1. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  2. ETL Pentaho Data Integration (Kettle) 插入/更新 问题 etl

    Pentaho Data Integration (Kettle) 使用此工具 按 索引  做 插入更新操作时,也可能报 索引重复 的错误, 解决方法:  匹配的索引字段可能有null值,会导致此错误 ...

  3. Apache Spark探秘:三种分布式部署方式比较

    转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/     目 ...

  4. MySQL 体系架构

    MySQL 体系架构 本篇文章是对mysql体系结构进行了详细的分析介绍,需要的朋友参考下 上面一图是mysql的概览图,我们从上往下看, 我们把上面一图一分为二,我们可以吧它分为两个部分, 1,是c ...

  5. 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  6. oracle——merge

      一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...

  7. Unit Test Generator

           

  8. Follow Path -》 Unity3d通用脚本

    PathDefinition.cs using UnityEngine; using System.Collections; using System.Collections.Generic; usi ...

  9. Unity3D研究院之脚本批量打包渠道包研究

    原地址:http://www.xuanyusong.com/archives/2418#comments 最近在研究Unity3D脚本批量打包,比如在Android平台下各种不同分辨率和不同内存大小的 ...

  10. Spring @ Component 的作用

    1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...