洛谷P1087FBI树

题目链接
洛谷P1087FBI树

输入输出样例
输入

3
10001011

输出

IBFBBBFIBFIIIFF

解析
由样例构造的FBI树如下,后序遍历为 I B F B B B F I B F I I I F F IBFBBBFIBFIIIFF 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值