S-Trees -uva

來源:互聯網
上載者:User

網上有的神牛這題沒建樹A出來了。。。仔細一想,還是老老實實建樹一遍AC了。。。(好像自己很懶)

#include<cstdio>#include<cstring>#include<iostream>#include<malloc.h>using namespace std;#define MAX_SIZE 10#define MAXD (1<<8)int m,n;int  ord[MAX_SIZE];int    x[MAX_SIZE];int  last_node[MAXD];int fin[MAXD];int pos;struct Tree{    int   value;/*節點的值*/    Tree *Left;    Tree *Right;};Tree *BuildTree(int u){ /*u展示層數*/    if(u == n){        Tree *node = (Tree*)malloc(sizeof(Tree));        node -> value = last_node[pos++];        node -> Left = NULL;        node ->Right = NULL;        return node;    }    else{        Tree *node = (Tree*)malloc(sizeof(Tree));        node -> value = x[ord[u]-1];        node -> Left  = BuildTree(u+1);        node -> Right = BuildTree(u+1);        return node;    }}void dfs(Tree *node,int u){    if(node -> Left == NULL){        fin[u] = node -> value;        return ;    }    else {        if(node -> value == 0)          dfs(node->Left,u);        else if(node -> value ==1)          dfs(node->Right,u);    }}int main(){    int Case = 1;    while(scanf("%d",&n)&&n){        char temp;        getchar();        memset(ord , 0, sizeof(ord));        memset(last_node ,0 , sizeof(last_node));        for(int i = 0;i < n; i++)        scanf("%*c%d%*c",&ord[i] );        for(int i = 0;i < (1<<n);i++){            scanf("%c",&temp);            last_node[i] = temp - '0';        }        scanf("%d",&m);        for(int k = 0;k < m;k++){            memset( x, 0, sizeof(x));            getchar();            for(int i = 0;i < n ; i++){                scanf("%c",&temp);                x[i] = temp - '0';            }            pos = 0;            Tree *root = BuildTree(0);            dfs(root,k);        }        printf("S-Tree #%d:\n",Case++);        for(int i = 0; i < m;i++)        printf("%d",fin[i]);        printf("\n\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.