题目
链接: 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);
}
}