题意理解
这条题目是这样的:
全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。
如果一个节点的字符串满足这样的性质,那么它的值就是FBI中的一个。然后需要将这个节点的字符串对半分去构建子树。
代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class Node {
String value;
char type;
public char getType() {
int zero = 0;
int one = 0;
for(int i = 0; i < value.length(); i++) {
if(value.charAt(i) == '0') {
zero++;
} else {
one++;
}
}
if(zero > 0 && 0 == one) {
return 'B';
} else if(0 == zero && one > 0){
return 'I';
} else {
return 'F';
}
}
Node left;
Node right;
public Node(String value) {
this.value = value;
this.type = getType();
int len = value.length();
if(len > 1) {
String leftValue = value.substring(0, len / 2);
String rightValue = value.substring(len / 2);
Node lc = new Node(leftValue);
Node rc = new Node(rightValue);
this.left = lc;
this.right = rc;
}
}
public void print() {
if(left != null) {
left.print();
right.print();
}
System.out.print(this.type);
}
}
public class Main {
public static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken() {
while(null == st || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.valueOf(nextToken());
}
}
static int N;
static String input;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
N = fs.nextInt();
input = fs.nextToken();
Node node = new Node(input);
node.print();
}
}