您的位置:首页 > 其它

【递归】Vijos P1114 FBI树(NOIP2004普及组第三题)

2016-04-18 17:39 363 查看
[b]题目链接:[/b]

  https://vijos.org/p/1114

[b]题目大意:[/b]

  把01串一分为二,左半边描述当前节点左子树,右半边描述右子树,子树全为1则为I节点,全为0则为B节点,混合则为F节点,直到当前串长度为1停止。

  给定01串,求FBI树后序。

[b]题目思路:[/b]

  【递归】

  每次操作先操作左子树,再操作右子树,之后统计左右子树01状态,按照要求得到当前节点是 F B I中的哪一个。

  由于输出后序,所以可以每次操作完左右子树后直接输出该节点,当前串长度为1则输出完返回。

//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps 1e-8
#define J 10000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 1504
using namespace std;
int n,m,lll,ans,cas;
int e[]={1,2,4,8,16,32,64,128,256,512,1024};
char s
;
char p[]={'B','I','F'};
int work(int l,int r)
{
int ll,rr,mid=(l+r)>>1;
if(l==r)
{
printf("%c",p[s[l]=='1']);
return (s[l]=='1');
}
ll=work(l,mid);
rr=work(mid+1,r);
if(ll==rr)
{
printf("%c",p[ll]);
return ll;
}
else
{
printf("%c",p[2]);
return 2;
}
}
int main()
{
#ifndef ONLINE_JUDGE
//    freopen("1.txt","r",stdin);
//    freopen("2.txt","w",stdout);
#endif
int i,j,k;
//    while(~scanf("%s",s1))
while(~scanf("%d",&n))
{
scanf("%s",s);
work(0,e
-1);
puts("");
}
return 0;
}

/*
//

//
*/


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: