爆栈了。。。。。。90分求助,最后一个MLE
查看原帖
爆栈了。。。。。。90分求助,最后一个MLE
684890
Rhss楼主2022/8/13 08:20
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
struct node{
	string data;
	int judge;//-1表示全1串,0表示全0串,1表示01串
	node* lchild;
	node* rchild;
};
void dfs(node* &root,string str){
	root = new node;
	root->data=str;
	root->lchild=root->rchild=nullptr;
	int len = str.size();
	int g = 0,r = 0;
	//寻找0和1是否都出现
	for(int i = 0;i<len;++i){
		if(str[i]=='0'){
			g=1;
		}
		if(str[i]=='1'){
			r=1;
		}
		if(g&&r){
			break;
		}
	}
	//F树
	if(g&&r){
		root->judge=1;
	}else{
		//B树
		if(g){
			root->judge=0;
		}else{
			//I树
			if(r){
				root->judge=-1;
			}			
		}		
	}
	if(str.size()!=1){
		string str_left = str.substr(0,len/2);
		string str_right = str.substr(len/2,len);
		dfs(root->lchild,str_left);
		dfs(root->rchild,str_right);
	}
	return ;
}
void postorder(node* root){
	if(root==nullptr){
		return ;
	}
	postorder(root->lchild);
	postorder(root->rchild);
	if(root->judge==1){
		cout<<"F";
	}else{
		if(root->judge==0){
			cout<<"B";
		}else{
			cout<<"I";
		}
	}
}
int main(){
	cin>>n>>s;
	node* root = new node;
	root->data=s;
	root->judge=1;
	root->lchild=root->rchild=nullptr;
	int len = s.size();
	string str_left = s.substr(0,len/2);
	string str_right = s.substr(len/2,len);
	dfs(root->lchild,str_left);
	dfs(root->rchild,str_right);
	postorder(root);
	return 0;
}

2022/8/13 08:20
加载中...