-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest
More file actions
59 lines (47 loc) · 1.57 KB
/
test
File metadata and controls
59 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.math.BigInteger;
import java.util.Scanner;
import java.util.stream.Stream;
public class Solution {
static class NumberTree {
final NumberTree[] childs = new NumberTree[10];
short flags;
public void add(final String numberString, final int i) {
final int index = numberString.charAt(i) - '0';
if (numberString.length() == i + 1) {
this.flags |= 1 << index;
return;
}
if (this.childs[index] == null) {
this.childs[index] = new NumberTree();
}
this.childs[index].add(numberString, i + 1);
}
public int count(final String numberString, final int i) {
final int index = numberString.charAt(i) - '0';
final int count = (this.flags & (1 << index)) > 0 ? 1 : 0;
if (this.childs[index] == null || numberString.length() == i + 1) {
return count;
}
return count + this.childs[index].count(numberString, i + 1);
}
}
static final NumberTree ROOT = new NumberTree();
public static void main(final String[] args) {
BigInteger bi = BigInteger.ONE;
for (int i = 0; i < 801; i++) {
ROOT.add(bi.toString(), 0);
bi = bi.multiply(BigInteger.valueOf(2));
}
try (final Scanner scanner = new Scanner(System.in)) {
final int t = Integer.parseInt(scanner.nextLine());
Stream.generate(scanner::nextLine).limit(t).mapToInt(Solution::twoTwo).forEach(System.out::println);
}
}
static int twoTwo(final String a) {
int count = 0;
for (int i = 0; i < a.length(); i++) {
count += ROOT.count(a, i);
}
return count;
}
}