LRJ-Example-06-17-Uva10562

main() 函数中的这两行

fgets(buf[0], maxn, stdin);
sscanf(buf[0], "%d", &T);

不能简单替换为

scanf("%d", &T);

因为这会影响后面用fgets()读取数的内容。这个scanf之后调用 fgets() 会读取一个空的行。

从样例输出里面可以看到

每棵树的最外侧有一对()

每个叶子节点后面是(),代表空树

#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int n = 0;
const int maxn = 200 + 5;
char tree[maxn][maxn];

bool isLabel(char c) {
    if( c == '-' || c == '|' || c == ' ' || c == '#' || c == '\n') return false;
    else return true;
}

void dfs(int row, int col)
{
    printf("%c(", tree[row][col]);
    if(row+1 < n && tree[row+1][col] == '|') { // [row][col] has a subtree
        //
        if(row+2 < n) { // ----
            // find leftmost '-' of this subtree
            int c = col;
            int l = strlen(tree[row+2]);
            while(c-1 >= 0 && tree[row+2][c-1] == '-') c--;

            //printf("\nleftmost - is at [%d][%d]\n", row+2, c);

            for(int i = c; i < l && tree[row+2][i] == '-'; i++) { // each child
                //if(row+3 < n && tree[row+3][i] != '\0' && !isspace(tree[row+3][i])) {
                if(row+3 < n && isLabel(tree[row+3][i]) && !isspace(tree[row+3][i])) {
                    dfs(row+3, i);
                }
            }
        }  
    }
    printf(")");
}

void solve() {
    memset(tree, 0, sizeof(tree));
    n = 0;
    while(true) {
        fgets(tree[n], maxn, stdin); // read each line
        if(tree[n][0] == '#') break;
        n++;
    }

    /* 
    for(int i = 0; i < n; i++) {
        printf("%s", tree[i]);
    }
    */

    printf("(");

    if(n > 0) { // tree is not empty
        int col = 0;
        int len = strlen(tree[0]);
        //while(col < len && tree[0][col] == ' ') col++;

        //dfs(0, col); //root is at [0][col]
        for(int i = 0; i < len; i++)
            if(tree[0][i] != ' ') {
                dfs(0, i);
                break;
            }
    }

    printf(")\n");
}

int main() {
    int T;
    fgets(tree[0], maxn, stdin);
    sscanf(tree[0], "%d", &T);
    while(T--) {
        solve();
    }
}

 

下面是 accept的代码

#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int n = 0;
const int maxn = 200 + 5;
char tree[maxn][maxn];

bool isLabel(char c) {
    if( c == '-' || c == '|' || c == ' ' || c == '#' || c == '\n') return false;
    else return true;
}

void dfs(int row, int col)
{
    printf("%c(", tree[row][col]);
    if(row+1 < n && tree[row+1][col] == '|') { // [row][col] has a subtree
        //
        if(row+2 < n) { // ----
            // find leftmost '-' of this subtree
            int c = col;
            int l = strlen(tree[row+2]);
            while(c-1 >= 0 && tree[row+2][c-1] == '-') c--;

            //printf("\nleftmost - is at [%d][%d]\n", row+2, c);

            for(int i = c; i < l && tree[row+2][i] == '-'; i++) { // each child
                if(row+3 < n && tree[row+3][i] != '\0' && !isspace(tree[row+3][i])) {
                //if(row+3 < n && isLabel(tree[row+3][i]) && !isspace(tree[row+3][i])) {
                    dfs(row+3, i);
                }
            }
        }  
    }
    printf(")");
}

void solve() {
    memset(tree, 0, sizeof(tree));
    n = 0;
    while(true) {
        fgets(tree[n], maxn, stdin); // read each line
        if(tree[n][0] == '#') break;
        n++;
    }

    /* 
    for(int i = 0; i < n; i++) {
        printf("%s", tree[i]);
    }
    */

    printf("(");

    if(n > 0) { // tree is not empty
        int col = 0;
        int len = strlen(tree[0]);
        //while(col < len && tree[0][col] == ' ') col++;

        //dfs(0, col); //root is at [0][col]
        for(int i = 0; i < len; i++)
            if(tree[0][i] != ' ') {
                dfs(0, i);
                break;
            }
    }

    printf(")\n");
}

int main() {
    int T;
    fgets(tree[0], maxn, stdin);
    sscanf(tree[0], "%d", &T);
    while(T--) {
        solve();
    }
}

 

posted @ 2018-01-22 16:56  PatrickZhou  阅读(199)  评论(0编辑  收藏  举报