1 条题解

  • 0
    @ 2024-12-24 9:54:34

    C :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct BTNode{
    	char data;
    	struct BTNode *left,*right;
    }BTNode,*BTree;
    void build(BTree *t,char *pre,char *in,int len){
    	if (len<=0)
    	{
    		*t=NULL;
    		return;
    	}
    	int pos=strchr(in,pre[0])-in;
    	*t=(BTNode *)malloc(sizeof(BTNode));
    	(*t)->data=*pre;
    	build(&(*t)->left,pre+1,in,pos);
    	build(&(*t)->right,pre+pos+1,in+pos+1,len-pos-1);
    }
    void postorder(BTree t){
    	if (t)
    	{
    		postorder(t->left);
    		postorder(t->right);
    		printf("%c",t->data);
    	}
    }
    void clean(BTree *t){
    	if (*t)
    	{
    		clean(&(*t)->left);
    		clean(&(*t)->right);
    		free(*t);
    	}
    }
    int main(){
    	BTree root;
    	char pre[30],in[30];
    //	freopen("1.txt","r",stdin);
    	while (scanf("%s %s",pre,in)!=EOF)
    	{
    		build(&root,pre,in,strlen(pre));
    		postorder(root);
    		clean(&root);
    		printf("\n");
    	}
    //	fclose(stdin);
    	return 0;
    }
    

    C++ :

    #include <stdio.h>
    #include <string.h>
    void run(char a[],char b[])
    {
    	int i,n,j;
    	char c[33],d[33];
    	n=strlen(a)-1;
    	if(n<=0)
    	{
    		printf("%s",a);
    		return;
    	}
    	i=0;
    	while(b[i]!=a[0])
    		i++;
    	for(j=0;j<i;j++)
    	{
    		c[j]=a[j+1];
    		d[j]=b[j];
    	}
    	c[i]=0;
    	d[i]=0;
    	run(c,d);
    	for(j=0;j<n-i;j++)
    	{
    		c[j]=a[j+i+1];
    		d[j]=b[j+i+1];
    	}
    	c[n-i]=0;
    	d[n-i]=0;
    	run(c,d);
    	printf("%c",a[0]);
    }
    int main()
    {
    	char a[33],b[33];
    	scanf("%s",a);
    	while(strcmp(a,"")!=0)
    	{
    		scanf("%s",b);
    		run(a,b);
    		printf("\n");
    		strcpy(a,"");
    		scanf("%s",a);
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    1273
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者