POJ 2255 Tree Recovery 树的遍历,分治 难度:0

http://poj.org/problem?id=2255

#include<cstdio>
#include <cstring>
using namespace std;
const int maxn = 27;
char pre[maxn],in[maxn];
char past[maxn];
void tre(int ps,int pe,int is,int ie,int& ind)
{
    int lnum = strchr(in,pre[ps]) - in - is;

    if(lnum != 0)
    {
        tre(ps + 1,ps + 1 + lnum,is ,is + lnum,ind);
    }
    if(is + lnum + 1 != ie)
    {
        tre(ps + 1 + lnum,pe,is + lnum + 1 ,ie,ind);
    }
    past[ind++] = pre[ps];
}
int main()
{
    while(scanf("%s%s",pre,in) == 2)
    {
        int ind = 0;
        int len = strlen(pre);
        tre(0,len,0,len,ind);
        past[len] = 0;
        printf("%s\n",past);
    }
    return 0;
}

 

posted @ 2015-07-22 23:15  雪溯  阅读(112)  评论(0编辑  收藏  举报