洛谷P1087FBI树

题目链接
洛谷P1087FBI树

输入输出样例
输入

3
10001011

输出

IBFBBBFIBFIIIFF

解析
由样例构造的FBI树如下,后序遍历为 I B F B B B F I B F I I I F F IBFBBBFIBFIIIFF

									F
					F								F
			F				B				F				I
		I		B		B		B		I		B		I		I				
	   	1		0		0		0		1		0		1		1

具体实现参考归并排序/二分的思想。

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int n;
string s;
void work(int l,int r){
	
//	if(l==r) return;	
	int mid=(l+r)>>1;
	if(l!=r)
	work(l,mid),work(mid+1,r);
	int flag1=0,flag2=0;
	for(int i=l;i<=r;i++){
		if(s[i-1]=='0')
			flag1=1;
		if(s[i-1]=='1')
			flag2=1;
	}
	if(flag1&&flag2) cout<<"F";
	else if(flag1&&!flag2) cout<<"B";
	else if(!flag1&&flag2) cout<<"I";
}
int main(int argc, char** argv) {
	cin>>n>>s;
	int n=s.length();
	work(1,n);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhuixun_/article/details/82821855
今日推荐