Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don’t know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor’s works were found in a 3.5” floppy disk left inside the drive.
The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek —we leave this trivial task for you. Convert professor’s trees to our trees.
Input
The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 ≤ T ≤ 20) drawn in the file. Then you would have T trees, each ending with a single hash (‘#’) mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols ‘-’, ‘|’, ‘ ’ (space) and ‘#’. Every node that has children has a ‘|’ symbol drawn just below itself. And in the next line there would be a series of ‘-’ marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.
Output
Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.
Sample Input
2
A
|
--------
B C D
| |
----- -
E F G
#
e
|
----
f g
#
Sample Output
(A(B()C(E()F())D(G())))
(e(f()g()))
题目类型:DFS先序遍历
算法分析:按照先序遍历的顺序递归输出即可,注意查找子节点时满足的条件!!
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
/************************************************** filename :a.cpp author :maksyuki created time :2018/6/4 15:10:46 last modified :2018/6/4 16:29:10 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 200 + 66; string sa[maxn]; int n; void dfs(int x, int y) { cout << sa[x][y] << "("; //DB(x), DB(y); if(x + 1 < n && sa[x+1][y] == '|') { int p = y; while(p - 1 >= 0 && sa[x+2][p-1] == '-') p--; while(sa[x+2][p] == '-' && sa[x+3][p]) { if(x + 3 < n && isprint(sa[x+3][p]) && sa[x+3][p] != ' ' && sa[x+3][p] != '#') dfs(x + 3, p); p++; } } cout << ")"; } int main() { #ifdef LOCAL CFF; CFO; #endif string s; getline(cin, s); stringstream ss(s); int t; ss >> t; string tmps; while(t--) { n = 0; for(int i = 0; i < maxn; i++) sa[i].clear(); while(getline(cin, tmps)) { if(tmps[0] == '#') break; sa[n++] = tmps; } cout << "("; for(int i = 0; sa[0][i]; i++) { if(isprint(sa[0][i]) && sa[0][i] != ' ' && sa[0][i] != '#') { dfs(0, i); break; } } cout << ")" << endl; } return 0; } |