-
Notifications
You must be signed in to change notification settings - Fork 34
/
A.java
65 lines (57 loc) · 2.15 KB
/
A.java
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
60
61
62
63
64
65
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* APAC 2015 Round A Problem A: Seven-segment Display
* Check README.md for explanation.
*/
public class Main {
private String solve(Scanner scanner) {
String[] clock=new String[] {"1111011", "1111111", "1110000", "1011111",
"1011011", "0110011", "1111001", "1101101", "0110000", "1111110"};
int[] nums=Arrays.stream(clock).mapToInt(s->Integer.parseInt(s, 2)).toArray();
int n=scanner.nextInt();
int[] data=new int[n];
for (int i=0;i<n;i++) data[i]=Integer.parseInt(scanner.next(), 2);
HashSet<String> set=new HashSet<>();
for (int i=0;i<=nums[1];i++) {
int[] curr=new int[10];
for (int j=0;j<10;j++) curr[j]=nums[j]&i;
set.addAll(check(curr, data));
}
return set.size()==1?set.iterator().next():"ERROR!";
}
private HashSet<String> check(int[] nums, int[] data) {
HashSet<String> set=new HashSet<>();
for (int i=0;i<=9;i++) {
boolean able=true;
for (int j=0;j<data.length;j++) {
if (data[j]!=nums[(i+j)%10]) {
able=false;
break;
}
}
if (able) {
set.add(toString(nums[(i+data.length)%10]));
}
}
return set;
}
private String toString(int v) {
StringBuilder builder=new StringBuilder(Integer.toBinaryString(v));
while (builder.length()<7) builder.insert(0, '0');
return builder.toString();
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}