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:

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:

For example, if the record of states is like:

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:

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 N states 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 com.wy.test01;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class Dashboard
{
private int origin[][] =
{ //A B C D E F G
{ 1, 1, 1, 1, 1, 1, 0 },// 0
{ 0, 1, 1, 0, 0, 0, 0 },// 1
{ 1, 1, 0, 1, 1, 0, 1 },// 2
{ 1, 1, 1, 1, 0, 0, 1 },// 3
{ 0, 1, 1, 0, 0, 1, 1 },// 4
{ 1, 0, 1, 1, 0, 1, 1 },// 5
{ 1, 0, 1, 1, 1, 1, 1 },// 6
{ 1, 1, 1, 0, 0, 0, 0 },// 7
{ 1, 1, 1, 1, 1, 1, 1 },// 8
{ 1, 1, 1, 1, 0, 1, 1 } // 9
};
public static void main(String[] args)
{
Dashboard db = new Dashboard();
db.process();
}
private void process()
{
// 1.读取文件
try
{
FileReader reader = new FileReader("in/A-large-practice.in");
//FileReader reader = new FileReader("in/A-small-practice.in");
BufferedReader br = new BufferedReader(reader);
String str = null;
br.readLine();
int kk=1;
while ((str = br.readLine()) != null)
{
for (int j = 0; j < 7; j++)
{
_wronglight[j] = true;
_zeros[j] = true;
}
// 2.读取n
String[] piks = str.split(" ");
int n = Integer.parseInt(piks[0]);
// 3.转化读取数组
int[][] nums = new int[n][7];
setNums(nums, n, piks);
// 4.判断可能的错误灯号
wrongLight(nums);
// 5.对非错误灯号进行匹配
if(AllBrokeAndSmallerThan3(n, kk))
{
pipei(nums,kk);
}
kk++;
}
br.close();
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean AllBrokeAndSmallerThan3(int n, int kk)
{
if(n<4)
{
return true;
}
for(int i=0;i<7;i++)
{
if(!_wronglight[i])
{
return true;
}
}
System.out.print("Case #"+kk+": 0000000\n");
return false;
}
private boolean _zeros[] =
{ true, true, true, true, true, true, true };
public void pipei(int[][] nums, int kk)
{
ArrayList<String> next_num=new ArrayList<String>();
for(int i=9;i>=0;i--)
{
for(int j=0;j<7;j++)
{
_zeros[j] = true;
}
int o_seq=i,k_seq=0;
boolean seq_fail = false;
while(k_seq<nums.length)
{
for(int j=0;j<7;j++)
{
if(!_wronglight[j])
{
if(origin[o_seq][j]!=nums[k_seq][j])
{
seq_fail = true;
break;
}
}
else
{
//如果原来的有1,就说明这个灯肯定坏了
if(origin[o_seq][j]!=0)
{
_zeros[j]=false;
}
}
}
if(seq_fail)
{
break;
}
k_seq++;
o_seq--;
if(o_seq<0)
{
o_seq=9;
}
}
if(!seq_fail)
{
for(int j=0;j<7;j++)
{ //灯坏不确定并且下一个不确定灯坏不坏的位置要输出1时,这种不确定是完全的不确定
if(_wronglight[j]&&_zeros[j]&&origin[o_seq][j]==1)
{
System.out.print("Case #"+kk+": ERROR!\n");
return;
}
}
String ss = "";
for(int j=0;j<7;j++)
{
if(!_wronglight[j])
{
ss+=origin[o_seq][j];
}
else
{
ss+="0";
}
}
next_num.add(ss);
}
}
System.out.print("Case #"+kk+": ");
//判断输出是否相同
for(int i=0;i<next_num.size();i++)
{
for(int j=i+1;j<next_num.size();j++)
{
if(!next_num.get(i).equals(next_num.get(j)))
{
System.out.print("ERROR!\n");
return;
}
}
}
System.out.print(next_num.get(0)+"\n");
}
// ====================4======================
private boolean _wronglight[] =
{ true, true, true, true, true, true, true };
public void wrongLight(int[][] nums)
{
for (int i = 0; i < nums.length; i++)
{
for (int j = 0; j < 7; j++)
{
if (nums[i][j] != 0)
{
_wronglight[j] = false;
}
}
}
}
// ====================3======================
private int cal10[] =
{ 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 };
private void setNums(int[][] nums, int n, String[] piks)
{
for (int i = 1; i <= n; i++)
{
int num = Integer.parseInt(piks[i]);
for (int j = 0; j < 7; j++)
{
int cache = (num % cal10[j]) / cal10[j + 1];
nums[i - 1][j] = cache;
}
}
}
}