#include <stdio.h> #include <string.h> #include <stdlib.h> char zhong[1000]; char hou[1000]; int len; char print[1000][1000]; char ptop[1000]; struct treenode { int top; char stack[1000]; struct treenode *lchild; struct treenode *rchild; }; struct treenode * create(struct treenode *t) { if(t->top==1) { t->lchild=NULL; t->rchild=NULL; } else if(t->top==0) t=NULL; else { char zuo[1000]; char you[1000]; int find=-1; for (int i=len-1; i>=0; --i) { for (int j=0; j<t->top; ++j) { if(hou[i]==t->stack[j]) { find=j; break; } } if(find!=-1) break; } if(find==-1) return t; //printf("%d\n",find); int k=0; for (int i=0; i<find; i++) { zuo[k++]=t->stack[i]; } zuo[k]='\0'; k=0; for (int i=find+1; i<t->top; ++i) { you[k++]=t->stack[i]; } you[k]='\0'; t->lchild=(struct treenode *)malloc(sizeof(struct treenode)); t->rchild=(struct treenode *)malloc(sizeof(struct treenode)); strcpy(t->lchild->stack,zuo); strcpy(t->rchild->stack,you); t->lchild->top=strlen(t->lchild->stack); t->rchild->top=strlen(t->rchild->stack); //puts(zuo); //puts(you); t->lchild=create(t->lchild); t->rchild=create(t->rchild); t->top=1; t->stack[0]=t->stack[find]; t->stack[1]='\0'; } return t; } void cengprinttree(struct treenode *t,int ceng) { if(t==NULL) return; if(t->stack[0]!='\0') print[ceng][ptop[ceng]++]=t->stack[0]; //printf("%c\n",t->stack[0]); cengprinttree(t->lchild,ceng+1); cengprinttree(t->rchild,ceng+1); return; } void printa() { for (int i=0; print[i][0]!='\0'; i++) { printf("%s",print[i]); } putchar('\n'); } int main() { memset(print,0,sizeof(print)); memset(ptop,0,sizeof(ptop)); gets(zhong); gets(hou); len=strlen(zhong); struct treenode *head; head=(struct treenode *)malloc(sizeof(struct treenode)); strcpy(head->stack,zhong); head->top=strlen(head->stack); head=create(head); cengprinttree(head,0); printa(); }