Falling Leaves

这里写图片描述
Sample Input

BDHPY
CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 10000+5
using namespace std;
struct Tree{
    char value;
    Tree *l,*r;
}*my;
Tree *creat(char x){
    Tree *t=(Tree *)new Tree;
    t->l=NULL;
    t->r=NULL;
    t->value=x;
    return t;
}
Tree *join(Tree *s,char x){
    Tree *t=(Tree *)new Tree;
    if(s==NULL){
        t=creat(x);
        s=t;
    }
    else {
        if(x<s->value)
            s->l=join(s->l,x);
        else
            s->r=join(s->r,x);
    }
    return s;
}
void put(Tree *s){
    if(s==NULL)
        return ;
    else {
        printf("%c",s->value);
        put(s->l);
        put(s->r);
    }
    return ;
}
int main(){
    string s,t;
    s="";
    while(cin>>t){
        if(t=="*"){
            my=NULL;
            for(int i=s.length()-1;i>=0;--i)
                   my=join(my,s[i]);
            put(my);
            putchar('\n');
            s="";
            continue;
        }
        else if(t=="$"){
            my=NULL;
            for(int i=s.length()-1;i>=0;--i)
                   my=join(my,s[i]);
            put(my);
            putchar('\n');
            break;
        }
        else s+=t;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42854569/article/details/81741485