【NOIP2001】【Luogu1030】求先序排列

problem

solution

codes

#include<stdio.h>
#include<string.h>
#define maxn 10
#define maxv maxn*30
char in[maxn], post[maxn];
char lch[maxv], rch[maxv];
int build(int l1, int r1, int l2, int r2){
    if(l1 > r1)return 0;
    char root = post[r2];
    int p = l1; while(in[p] != root)p++;
    int cnt = p-l1;
    lch[root] = build(l1,p-1,l2,l2+cnt-1);
    rch[root] = build(p+1,r1,l2+cnt,r2-1);
    return (int)root;
}
void dfs(char root){
    printf("%c",root);
    if(lch[root])dfs(lch[root]);
    if(rch[root])dfs(rch[root]);
}
int main(){
    scanf("%s%s",in+1,post+1);
    int n = strlen(in+1);
    build(1,n,1,n);
    dfs(post[n]);
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444693.html