P1030 求先序排列 【已知中序后序求先序】

题目

https://www.luogu.com.cn/problem/P1030

 

 

 代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
char in[20], post[20];
void run(char in[],char post[],int len)
{
    if (len <= 0)return;
    int root = post[len - 1];
    int i;
    for ( i = 0; i < len; i++)
    {
        if (in[i] == root)break;
    }
    printf("%c", root);
    run(in, post, i);
    run(in + i + 1, post+i, len-i -1);

}

int main()
{
    string a, b;
    cin >> a >> b;
    for (int i = 0; i < a.length(); i++)
        in[i] = a[i];
    for (int i = 0; i < b.length(); i++)
        post[i] = b[i];
    int len = a.length();
    run(in, post, len);
}

 

posted @ 2020-06-28 09:30  Jason66661010  阅读(167)  评论(0编辑  收藏  举报