Topic Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_ problem&problem=1503
Type of topic: data structure, two fork tree
Input and output:
Sample Professor ' s trees
2
A
|
--------
B C D
| |
----- -
E F G
#
E
|
----
f g
#
corresponding Our Trees
(A (B () C (E () F ()) D (G ()))
(E (f () g ()))
The main effect of the topic:
The input on the left represents a tree, and a node of the tree can have many sons. All inputs, as long as printable characters, and not spaces, ' # ', ' | ', '-' are all represented by a node.
If a node has a ' | ' below it. It means he has a son. His sons were in a continuous '-' node below.
Analysis and Summary:
If you use the method of achievement, the first look may be more complex, but in fact it is not difficult. My approach is to construct a node with a MAXN son pointer, and then open a two-dimensional array of the same size as the largest input dimension, which corresponds
To the input two-dimensional string array.
Then the output is enumerated, when encountering a character that is a node, the node should be the relative knot point to find a son, find a son with a function, search down all of its sons.
After the structure is finished, the tree is followed by the output, and the position of the left and right brackets is noted.
Logically speaking this problem as long as it is not difficult, but is disgusting to, last night began to do, think good idea soon after knocking good code, after the example confidently full of submission, and then, see the blood-red wrong answer, gave me blow. After the constant debugging, constantly WA.
And then on the Internet to find test samples, the results found that all can be passed. This means that it must not be my idea of the problem, it must be some details or boundary conditions are not considered.
Baidu, Google was I turned over dozens of pages, the results found that the online problem-solving reports are all using recursive search solution, and completely did not and I solve the same ideas. Even more wonderful is, my teammates also thought a very wonderful method, the result we all two cards for a long time.
The constant WA, gory fact, shows that even if you think you are right, make sure it is foolproof, there may be some nasty boundaries or your own thinking loophole and WA.
#include <cstdio> #include <cctype> #include <iostream> #include <cstring> #define MAXN 205
using namespace Std;
Class node{Public:char data;
Node *SON[MAXN];
};
Node *root;
Node node[210][210];
Char str[210][210];
int row; inline bool Isnode (char ch) {if (isprint (CH) && ch!= '-' && ch!= ' | ' && ch!= ' &
amp;& ch!= ' # ') return true;
return false;
} void Linkson (int r, int c) {if (r+3 >= row | | str[r+1][c]!= ' | ') return;
int i,j;
Move to the leftmost for '-' (i=c; str[r+2][i]== ' &&i>=0;-I.);;
int sonnum=0;
i++;
From the leftmost '-' to the right to the rightmost '-'. This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45528.htm//Here special attention to the boundary conditions, because this i<strlen (str[r+2) &&i<strlen (str[r+3])//Cause I'm depressed one Night + one morning for (; str[r+2][i]== '-' &&i<strlen (str[r+2)) & &i<strlen (str[r+3]); ++i) {if (Isnode (Str[r+3][i])) {//If node, link up node[r][c].son[sonnum++] = &node[r+3][i];
}} void Dfs (Node *root) {if (root) {printf ("%c", root->data);
BOOL flag = TRUE;
for (int j=0; j<maxn; ++j) {if (root->son[j]!=null) {flag = false;
if (j==0) printf ("(");
DFS (Root->son[j]);
} if (flag) printf ("()");
else printf (")");
} void Solve () {//initialization for (int i=0; i<row; ++i) {for (int j=0; J<strlen (str[i)); ++j) {
for (int k=0; k<maxn; ++k) node[i][j].son[k] = NULL;
} root = NULL;
for (int i=0; i< row; ++i) {for (int j=0; J<strlen (str[i)); ++j) {if (Isnode (str[i][j))) { if (root==null) {node[i][j].data = Str[i][j];
root = &node[i][j];
Linkson (I,J);
Break
} else{node[i][j].data = Str[i][j];
Linkson (i, j);
printf ("(")}}};
DFS (root);
printf (") \ n");
int main () {//Freopen ("Input.txt", "R", stdin);
int T;
scanf ("%d%*c", &t);
while (t--) {row=3;
memset (Str[2], 0, sizeof (str[2));
while (gets (Str[row])) {if (str[row][0]== ' # ') break;
++row;
memset (Str[row], 0, sizeof (str[row));//Initialize to avoid leaving the last data down} Solve ();
return 0; }