#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
//========函数中间有错误======
void xian(int n,char *s1,char *s2,char *res)
{
if(n<=0)return;
int p=strchr(s2,s1[n-1])-s2; //由s1[n-1]中知道树的根节点,求出该根节点在s2中的位置,减去s2的值得到左子树的长度
res[n-1]=s2[p]; // 这里有疑问
xian(p,s1,s2,res); // 左子树递归
xian(n-p-1,s1+p+1,s2+p+1,res+p); //右子树递归
}
int main()
{
char s1[1000]={0};
char s2[1000]={0};
char res[1000]={0};
// 用数组保存
int time;
cin>>time;
while(time--)
{
cin>>s1>>s2;//s1为后序,s2为中序
int len=strlen(s1);
xian(len,s1,s2,res);
cout<<res<<endl;
}
return 0;
}样例:
输入:
1
ACBFGED
ABCDEFG
输出:
DBACEGF
求解,函数里哪里错了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2001/viewspace-3129573/ ,如需转载,请注明出处,否则将追究法律责任。