usaco American Heritage

来源:互联网 发布:大数据分析 银行 编辑:程序博客网 时间:2024/05/16 03:43

这道题可以用来练习编码!

代码如下:

/*ID: guo geerPROG: heritageLANG: C++*/#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<fstream>using namespace std;struct Node{       char v;       Node *left;       Node *right;};int len;char pre[30], in[30];Node *head = new Node;int strIndex(int startIndex, int endIndex, char obj){    for(int i=startIndex; i<=endIndex; i++)    if(in[i] == obj) return i;}void buildTree(Node *&h, int a, int b, int m, int n){     if(a > b || m > n) return;     h = new Node;     h->v = pre[m];     h->left = NULL;     h->right = NULL;     //printf("%d %d %d %d %c" ,a, b, m, n, h->v);     //system("pause");     int index = strIndex(a, b, pre[m]);     buildTree(h->left, a, index-1, m+1, m+index-a);     buildTree(h->right, index+1, b, m+index-a+1, n);}void postOrder(Node *h){     if(h == NULL) return;     postOrder(h->left);     postOrder(h->right);     printf("%c", h->v);}int main(){    freopen("heritage.in", "r", stdin);    freopen("heritage.out", "w", stdout);    while(scanf(" %s", in) == 1)    {           scanf(" %s", pre);           len = strlen(in);           buildTree(head, 0, len-1, 0, len-1);           postOrder(head);           printf("\n");    }    return 0;}


原创粉丝点击