#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;
}
【POJ2255】Tree Recovery解題報告 思路 + 數據 +代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.