Written by razrlele
15:47 February 12, 2015
应该是UVa的原因,样例输入的格式显示似乎有一点偏差,其实真正的输入是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
2 A | -------- B C D | | ----- - E F G # e | ---- f g # |
意思就是如果字符正下方有一个”|”则表示这个字符是一个父节点,然后对应的”|”下面的”-“是用来区分不同子树的,只要是在连续的单杠符号下面的两个字符就属于同一个父节点,然后每一行之间都是连续的没有空行。遍历的时候注意把遍历过的单杠赋一个标记值就可以方便地区分子树了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/******************************************************** * File Name: uvaoj10562.cpp * Author: razrLeLe * Mail: razrlele@gmail.com * Homepage: https://yueyu.io * Created Time: Thu 12 Feb 2015 11:21:20 AM CST ******************************************************/ #include "vector" #include "set" #include "deque" #include "queue" #include "algorithm" #include "functional" #include "iostream" #include "cstdio" #include "cmath" #include "cstdlib" #include "string" #include "cstring" #include "string.h" #include "map" #include "cctype" #include "list" #include "stack" using namespace std; #define INF 0x3f3f3f3f #define LOCAL const int maxn = 205; char in[maxn][maxn]; int n; void solve(int row, int col) { if(in[row][0] == '#') return ; printf("%c(", in[row][col]); int border_start = 0, border_end; if(row+1 < n && in[row+1][col] == '|') { while(border_start < maxn && in[row+2][border_start] != '-') border_start++; border_end = border_start; while(border_end < maxn && in[row+2][border_end] == '-') border_end++; if(row+3 < n) { for(int i = border_start; i < border_end; i++) { in[row+2][i] ='*';//对边界赋标记值 if(in[row+3][i] != ' ' && in[row+3][i] != '\0') solve(row+3, i); } } } printf(")"); return; } int main() { #ifdef LOCAL freopen("/home/razrlele/build/data.txt", "r", stdin); freopen("/home/razrlele/build/out.txt", "w", stdout); #endif int numcase; scanf("%d", &numcase); getchar(); while(numcase--) { memset(in, '\0', sizeof(in)); n = 0; while(cin.getline(in[n], maxn) && in[n][0] != '#' ) n++; int p = 0; printf("("); while(p < maxn && in[0][p] == ' ') p++; solve(0, p); printf(")\n"); } return 0; } |