https://pintia.cn/problem-sets/994805046380707840/problems/1336215880692482054
解析
其实一开始我的思路是用二叉树的形式去实现,因为我看只有 8 个结点,并且我很快的就用代码实现了,但后面我看输入描述,发现它居然要求 N 次提问,也就是树的深度要为 N,当 N = 30 N=30 N=30 时,关树的叶子结点就需要 2 30 2^{30} 230 个,我果断放弃这个思路…
使用二进制的思路实现,关系如下:
输入 二进制 十进制 +1
yny 010 2 3
nyy 100 4 5
nyn 101 5 6
yyn 001 1 2
将 y 看成 0,n 看成 1 转为十进制加一后就是答案。
这里我为了方便,就直接用 replaceAll 了(其实是因为想用正则了… 😂)
AC Code
public static void main(String[] args) throws Exception {
int n = nextInt(), m = nextInt();
while(m-- != 0) {
String str = br.readLine();
str = str.replaceAll("y", "0");
str = str.replaceAll("n", "1");
BigInteger val = new BigInteger(str, 2);
out.println(val.add(BigInteger.ONE));
}
out.flush();
}