您的位置:首页 > 其它

ACM篇:UVa 10562 -- Undraw the Trees

2017-01-11 12:52 381 查看
#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
) && strcmp(s
, "#"))
n++;
}
void print_tree(int r, int lo, int hi)
{
putchar('(');
for (int j = lo; j <= hi; j++)
{
if (is_printable(s[r][j]))
{
putcha
926c
r(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: