http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3735

给了二叉树的先序遍历给成后续遍历。。

解题: 分而治之。。

View Code
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn=100000;
int number[maxn];
void solve(int s,int e)
{
    if(s==e)
    {cout<<number[s]<<endl;return ;}
    int i=s;
    for( i=s+1;i<=e;i++)
        if(number[i]>number[s])
        break;
    if(s+1<=i-1)solve(s+1,i-1);
    if(i<=e)solve(i,e);
    cout<<number[s]<<endl;
}
int main()
{
    int i=-1;
    while(scanf("%d",&number[++i])!=EOF);
    solve(0,i-1);
    return 0;
}

 

posted on 2012-10-05 00:16  一把刷子  阅读(229)  评论(0编辑  收藏  举报