P1030 求先序排列 -求助【c++】
查看原帖
P1030 求先序排列 -求助【c++】
366179
dengyujie2020楼主2022/8/5 14:09

https://www.luogu.com.cn/record/82416257 代码:

#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int n;
void dfs(int l1,int r1,int l2,int r2)
{
	if(l1>r1||l2>r2||l1<0||r1<0||l2<0||r2<0) return ;
	cout<<s2[r2];//输出根
	int x=s1.find(s2[r2]);//根在s1中的位置 
	if(s1[l1]!=s2[r2])//有左 
	{
		int xx=s2.find(s1[x-1]);//左在s2中结束位置 
		dfs(l1,x-1,l2,xx);
	}
	if(x!=r2)//有右 
	{
		int xx=s2.find(s1[x+1]);//右在s2中开始位置
		dfs(x+1,r1,xx,r2-1); 
	}
}
int main()
{
	cin>>s1;
	cin>>s2;
	n=s1.size();
	dfs(0,n-1,0,n-1);
	return 0;
}

80分,一个测试点WA

烦请各位大佬看看哪里不足,多谢!

2022/8/5 14:09
加载中...