Problem A. Seven-segment Display
Small input 8 points |
|
Large input 14 points |
|
Problem
Tom is a boy whose dream is to become a scientist, he invented a lot in his spare time. He came up with a great idea several days ago: to make a stopwatch by himself! So he bought a seven-segment display immediately.
The seven elements of the display are all light-emitting diodes (LEDs) and can be lit in different combinations to represent the arabic numerals like:
![](https://code.google.com/codejam/contest/images/?image=digits_30.png&p=5768691975192576&c=3214486)
However, just when he finished the programs and tried to test the stopwatch, some of the LEDs turned out to be broken! Some of the segments can never be lit while others worked fine. So the display kept on producing some ambiguous states all the time...
Tom has recorded a continuous sequence of states which were produced by the display and is curious about whether it is possible to understand what this display was doing. He thinks the first step is to determine the state which the display will show next, could you help him?
Please note that the display works well despite those broken segments, which means that the display will keep on counting down cyclically starting from a certain number (can be any one of 0-9 since we don't know where this record starts from). 'Cyclically' here means that each time when the display reaches 0, it will keep on counting down starting from 9 again.
For convenience, we refer the seven segments of the display by the letters A to G as the picture below:
![](https://code.google.com/codejam/contest/images/?image=marks_40.png&p=5768691975192576&c=3214486)
For example, if the record of states is like:
![](https://code.google.com/codejam/contest/images/?image=example_in_30.png&p=5768691975192576&c=3214486)
It's not that hard to figure out that ONLY segment B is broken and the sequence of states the display is trying to produce is simply "9 -> 8 -> 7 -> 6 -> 5". Then the next number should be 4, but considering of the brokenness of segment B, the next state should be:
![](https://code.google.com/codejam/contest/images/?image=example_out_30.png&p=5768691975192576&c=3214486)
Input
The first line of the input gives the number of test cases, T. Each test case is a line containing an integer N which is the number of states Tom recorded and a list of the Nstates separated by spaces. Each state is encoded into a 7-character string represent the display of segment A-G, from the left to the right. Characters in the string can either be '1' or '0', denoting the corresponding segment is on or off, respectively.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1). If the input unambiguously determines the next state of the display, y should be that next state (in the same format as the input). Otherwise, y should be "ERROR!".
Limits
1 ≤ T ≤ 2000.
Small dataset
1 ≤ N ≤ 5.
Large dataset
1 ≤ N ≤ 100.
Sample
package codejam;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class SevenSegmentDisplay {
public static byte[] seg = {
0X7E,
0X30,
0X6D,
0X79,
0X33,
0X5B,
0X5F,
0X70,
0X7F,
0X7B
};
public static void main(String[] args){
try{
solveSevensegmentDisplay();
}
catch (IOException e){
e.printStackTrace();
}
}
public static void solveSevensegmentDisplay() throws IOException{
File file = new File("E:\\A-large-practice.in");
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\A-large-practice.out"));
int T = Integer.parseInt(br.readLine());
byte[] history = new byte[100];
for(int t=1; t<=T; t++){
String[] str = br.readLine().split(" ");
int N = Integer.parseInt(str[0]);
int result=0, preResult=0;
byte broken, good, unsure=0X00;
boolean match=false;
boolean err = false;
for(int i=1; i<=N; i++){
byte display=0;
for(int j=0;j<7;j++){
display<<=1;
display|=(str[i].charAt(j)=='1' ? 1 : 0);
}
history[i-1] = display;
if(i>=11&&!str[i].equals(str[i-10])){
err = true;
break;
}
}
if(err==true){
bw.write("Case #" + t +": ERROR!\n");
continue;
}
for(int start=0; start<10; start++){
broken=0;
good=0;
boolean fail = false;
for(int i=0; i<N&&i<10; i++){
good|=history[i];
byte expect = seg[(start-i+10)%10];
broken|=(expect & ~history[i]);
unsure = (byte) (~good & ~broken & 0X7F);
if((good&broken)!=0 || (history[i]&~(expect&good))!=0){
fail = true;
break;
}
}
if(!fail){
result = seg[(start-N%10+10)%10]&~broken;
if((result&unsure)!=0 || (match&&(preResult!=result))){
match = false;
break;
}else{
match = true;
preResult = result;
}
}
}
if(match){
StringBuilder temp = new StringBuilder();
for(int i=6; i>=0; i--){
temp.append((result&1<<i)!=0 ? 1:0);
}
bw.write("Case #" + t +": "+temp+"\n");
}
else{
bw.write("Case #" + t +": ERROR!\n");
}
}
br.close();
bw.close();
}
}