题意:给你画了一颗树,你要把它的前序输出。

题解:读进到二维数组。边解析边输出。

坑:少打了个-1.

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<cstring>
  3. #include<cctype>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<string>
  7. #include<stack>
  8. #include<list>
  9. #include<set>
  10. #include<map>
  11. #include<queue>
  12. #include<vector>
  13. #include<sstream>
  14. #include<iostream>
  15. #include<algorithm>
  16.  
  17. using namespace std;
  18. const int maxn = + ;
  19. char buf[maxn][maxn];
  20. int n;
  21. void dfs(int r, int c) {//打印以[r][c]为根的树
  22. printf("%c(", buf[r][c]);
  23. if (r + < n&&buf[r + ][c] == '|') {
  24. int i = c;
  25. while (i - >= && buf[r + ][i-] == '-')i--;
  26. while (buf[r + ][i] == '-'&&buf[r + ][i] != '\0') {
  27. if (!isspace(buf[r + ][i])) dfs(r + , i);//fgets 到的'\n'也能i是space掉。
  28. i++;
  29. }
  30.  
  31. }
  32. cout << ')';
  33. }
  34. void solve() {
  35. n = ;
  36. for (;;) {
  37. fgets(buf[n], maxn, stdin);
  38. if (buf[n][] == '#')break; else n++;
  39. }
  40. printf("(");
  41. if (n) {
  42. for (int i = ; i < strlen(buf[]); i++) {
  43. if (buf[][i] != ' ') { dfs(, i); break; }
  44. }
  45. }
  46. cout << ')' << endl;
  47. }
  48. int main() {
  49. int t;
  50.  
  51. fgets(buf[], maxn, stdin);
  52. sscanf(buf[], "%d", &t);
  53. while (t--) {
  54. solve();
  55. }
  56. system("pause");
  57. return ;
  58. }
  59. /*
  60. 2
  61. A
  62. |
  63. --------
  64. B C D
  65. | |
  66. ----- -
  67. E F G
  68. #
  69. e
  70. |
  71. ----
  72. f g
  73. #
  74.  
  75. (A(B()C(E()F())D(G())))
  76. (e(f()g()))*/

【紫书】Undraw the Trees UVA - 10562 递归,字符串的更多相关文章

  1. 看图写树 (Undraw the Trees UVA - 10562)

    题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...

  2. 【紫书】Urban Elevations UVA - 221 离散化

    题意:给你俯视图,要求依次输出正视图中可以看到的建筑物 题解:任意相邻的x间属性相同,所以离散化. 坑:unique只能对数组用.下标易错 list不能找某元素的next.用了个很麻烦的处理 数组: ...

  3. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  4. 【紫书】Oil Deposits UVA - 572 dfs求联通块

    题意:给你一个地图,求联通块的数量. 题解: for(所有还未标记的‘@’点) 边dfs边在vis数组标记id,直到不能继续dfs. 输出id及可: ac代码: #define _CRT_SECURE ...

  5. 【紫书】 Unix ls UVA - 400 模拟

    题意:中文版https://vjudge.net/problem/UVA-400#author=Zsc1615925460 题解:首先读取字符,维护一个最长字符串长度M,再排序. 对于输出,写一个pr ...

  6. 紫书 例题8-12 UVa 12627 (找规律 + 递归)

    紫书上有很明显的笔误, 公式写错了.g(k, i)的那个公式应该加上c(k-1)而不是c(k).如果加上c(k-1)那就是这一次 所有的红气球的数目, 肯定大于最下面i行的红气球数 我用的是f的公式, ...

  7. UVa 1339,紫书P73,词频

    题目链接:https://uva.onlinejudge.org/external/13/1339.pdf 紫书P73 解题报告: #include <stdio.h> #include ...

  8. 紫书 例题 11-13 UVa 10735(混合图的欧拉回路)(最大流)

    这道题写了两个多小时-- 首先讲一下怎么建模 我们的目的是让所有点的出度等于入度 那么我们可以把点分为两部分, 一部分出度大于入度, 一部分入度大于出度 那么显然, 按照书里的思路,将边方向后,就相当 ...

  9. 【紫书】【重要】Abbott's Revenge UVA - 816 bfs 复杂模拟 带方向参数的迷宫

    题意:一个迷宫,每个交叉路口有一路标,限制了你从某方向进入该路口所能进入的路口. 题解:1.对于方向的处理:将node多增加一维dir,通过一个const 字符数组 加 上dir_id函数 以及一个方 ...

随机推荐

  1. MapWinGIS------引发类型为“System.Windows.Forms.AxHost+InvalidActiveXStateException”的异常

    转载:http://www.cnblogs.com/dzone/archive/2011/09/08/2171445.html

  2. centos7修改root密码

    1.重启系统,在下面界面时按e键 2.出现可编辑新内容,按向下键向下滑动,找到ro,并修改为rw 后,在LANG=en_US.UTF-8后面再加init=/bin/sh,结果如下图 3.然后按下ctr ...

  3. Spring boot配置log4j输出日志

    1. pom.xml文件中配置parent,版本选定[1.2.5.RELEASE] 关于为什么要选这个版本:我尝试使用[1.4.1.RELEASE],但该版本库里没有[spring-boot-star ...

  4. 【NLP】pyhanlp flask

    D:\ProgramData\Anaconda3\Lib\site-packages\pyhanlp\__init__.py 加入 WordVectorModel = LazyLoadingJClas ...

  5. springboot测试service层的单元测试

    package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import o ...

  6. RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败

    问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...

  7. (原创)Android Binder设计与实现 - 实现篇(1)

    本文属于原创作品,转载请注明出处并放于明显位置,原文地址:http://www.cnblogs.com/albert1017/p/3849585.html 前言 在学习Android的Binder机制 ...

  8. QT——信号槽

    结合教程,写出如下关于信号槽的代码,将教程中信号槽两种方式写入同一个界面中. #include "mainwindow.h" #include <QApplication&g ...

  9. IOS设计模式第八篇之键值观察模式

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 键值观察模式: 在KVO,一个对象可以要求被通知当他的某个特殊的属性被改变了.自己或者另一个对象.如果你感兴趣你可以阅读更多的信息参考: Apple ...

  10. 题目1440:Goldbach's Conjecture(哥达巴赫猜想)

    题目链接:http://ac.jobdu.com/problem.php?pid=1440 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...