求救
查看原帖
求救
1071829
ssssbbbbbb楼主2024/6/30 13:34
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+10;
struct node{
	int lc,rc,val;
}tree[maxn];
int n,m,k;
string z,h;
void dfs(int zl,int zr,int hl,int hr){
	k++;
	//cout<<k<<endl;
	tree[k].val=h[hr];
	if(zl==zr) return;//边界 
	int cnt=0;
	for(int i=zl;i<=zr;i++){
		if(h[hr]==z[i]) break;
		else cnt++;
	}
	tree[k].lc=k+1;
	tree[k].rc=k+cnt+1;
	if(cnt==0){
		tree[k].lc=0;
		dfs(zl+1,zr,hl,hr-1);
	}else if(h[hr]==z[zr]){
		tree[k].rc=0;
		dfs(zl,zr-1,hl,hr-1);
	}else{
		dfs(zl,zl+cnt,hl,hl+cnt);
		dfs(zl+cnt+1,zr,hl+cnt+1,hr-1);
	}
}
void dfs2(int u){	
	if(u==0) return ;
	cout<<tree[u].val;
	dfs2(tree[u].lc);
	dfs2(tree[u].rc);
}
int main(){
	cin>>z>>h;
	dfs(0,z.size()-1,0,h.size()-1);
	dfs2(1);
	return 0;
}
2024/6/30 13:34
加载中...