Google 2015 apac test Problem A, Sevensegment Display

Problem A. Seven-segment Display

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
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:

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

Input

4
1 1111111
2 0000000 0001010
3 0100000 0000111 0000011
5 1011011 1011111 1010000 1011111 1011011

Output
  Case #1: 1110000
Case #2: ERROR!
Case #3: 0100011
Case #4: 0010011

解法: 如果n>=10, 结果就是 第(n%10)个输入,否则,计算输入数码管中段位是0的集合x,
枚举x的所有子集,检测这个子集和输入的残缺数码管序列是否可以构成完整的数码管的递减序列,
如果可以计算下一个残缺的数码管的值,如果这样的值只有一个就输出它,否则输出ERROR!.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <map>
 4 #include <iostream>
 5 using namespace std;
 6 int a[120];
 7 int digit[10];
 8 
 9 int main(){
10     freopen("A-large-practice.in","r",stdin);
11     freopen("A-large-practice.txt","w",stdout);
12     int t;
13     scanf("%d",&t);
14     digit[0] = 126;
15     digit[1] = 48;
16     digit[2] = 109;
17     digit[3] = 121;
18     digit[4] = 51;
19     digit[5] = 91;
20     digit[6] = 95;
21     digit[7] = 112;
22     digit[8] = 127;
23     digit[9] = 123;
24     
25     for(int ks = 1; ks <= t; ks++){
26         printf("Case #%d: ",ks);
27         int n;
28         scanf("%d",&n);
29         char s[10];
30         int x = 0;
31         for(int i=0;i<n;i++){
32             a[i]=0;
33             scanf("%s",s);
34             for(int j=0;j<7;j++){
35                 a[i]<<=1;
36                 a[i] += s[j]=='1';
37             }
38             x|=a[i];
39         }
40         x=~x;
41         unsigned int msk = -1;
42         msk>>=25;
43         x&=msk;
44         if(n>=10){
45             int p = 1<<6;
46             while(p){
47                 if(p&a[n%10])putchar('1');
48                 else putchar('0');
49                 p>>=1;
50             }
51             putchar('\n');
52             continue;
53         }
54             int sub = x+1;
55             int ans = 0;
56             map<int,int> mp;
57             do{
58                 sub = (sub-1)&x;
59                 int y = sub|a[0];
60                 for(int i=9;i>=0;i--)
61                 {
62                     if(((y&digit[i])==digit[i])&&((a[0]|digit[i])==digit[i]))
63                     {
64                         int cnt = 1;
65                         for(int j = 1;j<n;j++){
66                             int tx = (i-j+10)%10;
67                             if((((sub|a[j])&digit[tx])==digit[tx])&&((a[j]|digit[tx])==digit[tx]))
68                             {
69                                 cnt++;
70                             }
71                             else break;
72                         }
73                         if(cnt==n)
74                         {
75                             ans = digit[(i-n+10)%10]&(~sub);
76                             mp[ans]++;
77                         }
78                     }
79                 }
80             }while(sub);
81             if(mp.size()==1)
82             {
83                 int p = 1<<6;
84                 while(p){
85                     if(p&ans)putchar('1');
86                     else putchar('0');
87                     p>>=1;
88                 }
89                 putchar('\n');
90             }
91             else 
92             {
93                 printf("ERROR!\n");
94             }
95         }
96     return 0;
97 }
 
原文地址:https://www.cnblogs.com/zhjou/p/4734921.html