ACM篇:UVa 10562 -- Undraw the Trees

来源:互联网 发布:ios存储数据的方法 编辑:程序博客网 时间:2024/05/23 00:00
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <cstring>#include <sstream>#include <cmath>#include <cctype>using namespace std;const int MAX = 200;int n;char s[MAX+2][MAX+2];bool is_printable(char ch){    return !(ch == '|' || ch == '-' || ch == ' ' || ch == '#' || ch == '\0' || ch == '\n');}int read_tree(){    memset(s, 0, sizeof(s));    n = 0;    while (gets(s[n]) && strcmp(s[n], "#"))        n++;}void print_tree(int r, int lo, int hi){    putchar('(');    for (int j = lo; j <= hi; j++)    {        if (is_printable(s[r][j]))        {            putchar(s[r][j]);            if (s[r+1][j] == '|')            {                int begin = j;                int end = j;                while (begin && s[r+2][begin-1] == '-')                    begin--;                while (s[r+2][end+1] == '-')                    end++;                  print_tree(r+3, begin, end);            }            else                 printf("()");        }    }           putchar(')');   }int main(){    int T;    scanf("%d\n", &T);    while (T-- > 0)    {        read_tree();        print_tree(0, 0, strlen(s[0])-1);        putchar('\n');    }    return 0;}
0 0
原创粉丝点击