【POJ2255】Tree Recovery解題報告 思路 + 資料 +代碼

來源:互聯網
上載者:User
#include <cstdlib>#include <cstdio>#include <iostream>#include <cstring>using namespace std;/*    Author: YuXun Lu    Time:23rd/2/2012    Last Time:About 7 Hours    Begin Time:Unknown    End Time:Unknown    Test Data:    ABDKQOPENLMCFIGHJ QKPODBNEMLACIFGJH    輸出:    隨便找一個AC過得程式測一下就好    思路:    以前序走訪的第一個元素將中序遍曆的數組分成兩個部分    [0..root)和(root,len-1]    有一個當前指標p,從1開始一直到len1-1遍曆前序數組。    p指向當前在中序遍曆搜尋的元素。    有一個node數組,儲存左右兒子的下標、自己的val在中序遍曆數組    中的位置還有一個val    Search_Left(int begin,int end,char val)        t = search(begin,end,val) //在中序數組裡搜尋當前要搜尋的節點        if( t != -1 ) 找到了        {          nodes[end].l = t;          p++          //搜尋找到的這個節點的左、右兒子,注意,          Search_Left的end是父節點的位置,          Search_Right的begin是父節點的位置          Search_Left(begin,nodes[t].pos,buf1[p]);          Search_Right(nodes[t].pos,end,buf1[p]);        }    Search_Right(int begin,int end,char val)    {        t = Search(begin,end,val)        if ( t != -1)        {           nodes[begin].r = t;           p++;           search_left(begin,nodes[t].pos,buf1[p]);           search_right(nodes[t].pos,end,buf1[p]);        }    }    注意,必須先搜左邊的再搜右邊的    因為每次搜尋的區間都不一樣,最好自己構建一棵樹寫一下這個    過程,區間的兩個端點是有繼承性的。*/const int MAX_SIZE = 1000;struct node{    int l; // the index of node in node    int r;    char val;    int pos;};char buf1[MAX_SIZE];char buf2[MAX_SIZE];int len1 = 0 ,len2 = 0,p;node nodes[MAX_SIZE];void left_search(int begin,int end,char val);void right_search(int begin,int end,char val);int search(int begin,int end,char val);void DFS(node now){    if(now.l != -1)      DFS(nodes[now.l]);    if(now.r != -1)      DFS(nodes[now.r]);    printf("%c",now.val);}int search1(int begin,int end,char val){    int res = -1;    for(int i = begin;i < end; i++)    {        if ( val == buf1[i])            res = i;    }    return res;}void left_search(int begin,int end,char val){    int t;    if(begin >= end)        return;#ifdef DBG    printf("Search left son for %c\n",nodes[end].val);#endif    t = search(begin,end,val);    if( t != -1)    {        ///找到了#ifdef DBG    printf("Gotted!Val is %c\n",buf2[t]);#endif        nodes[end].l = t;        p++; ///找下一個元素        left_search(begin,nodes[t].pos,buf1[p]);        right_search(nodes[t].pos,end,buf1[p]);    }}void right_search(int begin,int end,char val){    int t;    if(begin >= end)     return;#ifdef DBG    printf("Search right son for %c\n",nodes[begin].val);#endif    t = search(begin,end,val);    if( t != -1)    {#ifdef DBG    printf("Gotted!The val is %c\n",buf2[t]);#endif        nodes[begin].r = t;        p++;       // right_search(nodes[t].pos,end,buf1[p]);        left_search(begin,nodes[t].pos,buf1[p]);        right_search(nodes[t].pos,end,buf1[p]);    }}int search(int begin,int end,char val){    int res = -1;    for(int i = begin; i < end; i++)    {        if(buf2[i] == val)        {            res = i;break;        }    }    return res;}int main(int argc,char* argv[]){    #ifdef INPUT        freopen("B:\\acm\\poj2255-Tree Recovery\\input.txt","r",stdin);    #endif    while(scanf("%s",buf1) != EOF)    {       int root = 0;        memset(nodes,0,sizeof(node)*MAX_SIZE);        p = 0;        scanf("%s",buf2);        len1 =strlen(buf1);len2 = strlen(buf2);        for(int i = 0 ; i < len1 ; i++)        {            nodes[i].val = buf2[i];            nodes[i].l = -1;            nodes[i].r = -1;            nodes[i].pos = i;        }        root = search(0,len2,buf1[0]);        p = 1;        left_search(0,root,buf1[p]);        right_search(root,len2,buf1[p]);        DFS(nodes[root]);        memset(buf1,0,sizeof(char)*MAX_SIZE);        memset(buf2,0,sizeof(char)*MAX_SIZE);        printf("\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.