【USACO 3.4】(heritage)American Heritage

---------PROB----------

Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear `tree in-order' and `tree pre-order' notations.

Your job is to create the `tree post-order' notation of a cow's heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes.

Here is a graphical representation of the tree used in the sample input and output:

      C 

    /       \ 

   B        G

  /   \       /

 A   D    H

/   \

E    F

The in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree.

The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree.

The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root.

----------Solution----------

树的三种遍历

应该是学OI的基础题了

代码果断复制以前的。。。这不是好习惯。。。不要学。。

------------Code-------------

/*

ID:zst_0111

TASK:heritage

LANG:C++

*/

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

char str[30],a[27],b[27];

 

void cut(int s1,int s2,int len)

{

     if (len<=0)

         return;

     if (len==1)

     {

         putchar(a[s1]);

         return;

         }

     int i;

     for (i=s1;i<=s1+len-1;i++)

         if (b[s2]==a[i]) break;

     cut(s1,s2+1,i-s1);

     cut(i+1,s2+i-s1+1,s1+len-1-i);

     putchar(b[s2]);

}

 

int main()

{

    freopen("heritage.in","r",stdin);

    freopen("heritage.out","w",stdout);

 

    int i,j,k,m,n;

    scanf("%s",str);

    int l=strlen(str);

    l=strlen(str);

    for (i=0;i<l;i++)

        a[i+1]=str[i];

    scanf("%s",str);

    for (i=0;i<l;i++)

        b[i+1]=str[i];

    cut(1,1,l);

    printf("\n");

    return 0;

}


posted on 2011-09-15 22:56  青色有角三倍速  阅读(133)  评论(0)    收藏  举报