1. 程式人生 > >POJ T2255 Tree Recovery

POJ T2255 Tree Recovery

題解:

    在紙上模擬建樹過程,找出下標之間的關係,推出遞推公式即可

程式碼:

#include<cstdio> 
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = 27;


void build(int n,char *pre,char *in){
	if(n == 0) return;
	int pos = strchr(in,pre[0]) - in;
	
	build(pos,pre+1,in);
	printf("%c",pre[0]);
	build(n-pos-1,pre+pos+1,in+pos+1);
}

int main(){
	char pre[MAXN],in[MAXN];
	while(~scanf("%s%s",pre,in)){
		int n = strlen(pre);
		build(n,pre,in);
		printf("\n");
	}
	return 0;
}