zoukankan      html  css  js  c++  java
  • 【紫书】Undraw the Trees UVA

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

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

    坑:少打了个-1.

    #define _CRT_SECURE_NO_WARNINGS
    #include<cstring>
    #include<cctype>
    #include<cmath>
    #include<cstdio>
    #include<string>
    #include<stack>
    #include<list>
    #include<set>
    #include<map>
    #include<queue>
    #include<vector>
    #include<sstream>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    const int maxn = 200 + 5;
    char buf[maxn][maxn];
    int n;
    void dfs(int r, int c) {//打印以[r][c]为根的树
        printf("%c(", buf[r][c]);
        if (r + 1 < n&&buf[r + 1][c] == '|') {
            int i = c;
            while (i - 1 >= 0 && buf[r + 2][i-1] == '-')i--;
            while (buf[r + 2][i] == '-'&&buf[r + 3][i] != '') {
                if (!isspace(buf[r + 3][i])) dfs(r + 3, i);//fgets 到的'
    '也能i是space掉。
                i++;
            }
            
        }
        cout << ')';
    }
    void solve() {
        n = 0;
        for (;;) {
            fgets(buf[n], maxn, stdin);
            if (buf[n][0] == '#')break; else n++;
        }
        printf("(");
        if (n) {
            for (int i = 0; i < strlen(buf[0]); i++) {
                if (buf[0][i] != ' ') { dfs(0, i); break; }
            }
        }
        cout << ')' << endl;
    }
    int main() {
        int t;  
            
            
            fgets(buf[0], maxn, stdin);
            sscanf(buf[0], "%d", &t);
            while (t--) {
                solve();
        }
        system("pause");
        return 0;
    }
    /*
    2
    A
    |
    --------
    B C D
       | |
     ----- -
     E F G
    #
    e
    |
    ----
    f g
    #
    
    (A(B()C(E()F())D(G())))
    (e(f()g()))*/
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    Git与GitHub的基本使用
    HTML&CSS基础-外边框
    HTML&CSS基础-内边框
    SHELL脚本编程变量输入
    GoLang基础数据类型-切片(slice)详解
    GoLang基础数据类型--->数组(array)详解
    SHELL脚本编程的运算符
    SHELL脚本编程变量详解
    HTML&CSS基础-边框简写属性
    GoLang基础数据类型--->字符串处理大全
  • 原文地址:https://www.cnblogs.com/SuuT/p/8823301.html
Copyright © 2011-2022 走看看