AC

♻️lzhh_lzhh32  •  3个月前


#include<bits/stdc++.h>
using namespace std;
string s1,s2;		//中序 后序
vector<int> a;
int ans=0;
void maketree(int l1,int r1,int l2,int r2){		//1中2后 
	char root=s2[r2];
	for(int f=l1;f<=r1;f++){
		if(s1[f]==root){		//中序中找到根节点 
			a.push_back(root-'0'); 
			maketree(l1,f-1,l2,l2+(f-1-l1));		//左子树 
			maketree(f+1,r1,r2-1-(r1-f-1),r2-1);		//右子树 
		}
	}
} 
int main(){
	cin>>s1>>s2;
	maketree(0,s1.size()-1,0,s2.size()-1);
	for(int i=0;i<a.size();i+=2) ans+=a[i];
	cout<<ans<<endl;
	return 0;
}

评论:

请先登录,才能进行评论