[NOIP]2004

题目

链接: https://ac.nowcoder.com/acm/contest/19859/P
题意:


输入

3
10001011

输出

IBFBBBFIBFIIIFF


#解析 :
 题目所求,类似于建线段树(完全二叉树),叶子节点有2^n个,如果是叶子节点直接判断是FBI的那个结点,如果是父结点,需要结合子节点来判断父结点的类型,以此类推。答案输出是一个有2 ^(n+1)-1个字符的字符串


AC代码 - - -

#solution1

import java.util.*;
import java.io.*;
public class Main {
    static String s,st = "";
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static int ini() throws IOException{
        int x=0,c=in.read();while(c<48||c>57){if(c==-1) return -1; c=in.read();}
        while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48);c=in.read();}return x;
    }
    static String ins() throws IOException{
        String s = "";
        char c = (char)in.read();
        while(c!='0'&&c!='1') c = (char)in.read();
        while(c=='0'||c=='1') { s += c; c=(char)in.read();}
        return s;
    }
    
    static char build(int l,int r) {
        if(l==r) {
            st += s.charAt(l)=='0'?'B':'I';
            return s.charAt(l)=='0'?'B':'I';
        }
        int mid = (l+r)/2;
        char c1 = build(l,mid);
        char c2 = build(mid+1,r);
        if(c1=='B'&&c2=='B') {
            st += 'B';
            return 'B';
        }else if(c1=='I'&&c2=='I') {
            st += 'I';
            return 'I';
        }else {
            st += 'F';
            return 'F';
        }
        
    }
    public static void main(String[] args) throws IOException{
        int n = ini();
        s = ins();
        char t = build(0,s.length()-1);
        System.out.println(st);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值