Seven-segment Display

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 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


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
整体思想就是暴力求解,但是这道题的大数据有很多坑,写的时候根本想不到啊!思维还是不够缜密。

可能出错的输入:

6 0000000 0000000 0000000 0000000 0000000 0000000
6 1111111 1110000 1011111 1011011 0110011 1111001
7 0010000 0010000 0010000 0010000 0010000 0010000 0010000
2 1011001 0110001
1 0000000
2 0000000 0000000
3 0000000 0000000 0000000
4 0000000 0000000 0000000 0000000
5 0000000 0000000 0000000 0000000 0000000
6 0000000 0000000 0000000 0000000 0000000 0000000
7 0000000 0000000 0000000 0000000 0000000 0000000 0000000
8 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
9 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
10 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
11 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
12 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
13 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
14 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000
15 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000 0000000

相应的结果为:

Case #1: 0000000
Case #2: 1101101
Case #3: ERROR!
Case #4: 1111001
Case #5: ERROR!
Case #6: ERROR!
Case #7: ERROR!
Case #8: 0000000
Case #9: 0000000
Case #10: 0000000
Case #11: 0000000
Case #12: 0000000
Case #13: 0000000
Case #14: 0000000
Case #15: 0000000
Case #16: 0000000
Case #17: 0000000
Case #18: 0000000
Case #19: 0000000

改了好久,终于改出来了,跟那些ACM大神们比真是有种想死的心了。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>

using namespace std;

string States[10] = {
    "1111110", // 0
    "0110000",
    "1101101",
    "1111001",
    "0110011",
    "1011011",
    "1011111",
    "1110000",
    "1111111",
    "1111011"
};

// is string num_str possible the number of "num" ?
bool is_possible_num(vector<int> &good, const string &num_str, int num)
{
    for (int k = 0; k < 7; k++)
    {
        if (good[k] > 0 && States[num][k] == '1' && num_str[k] != '1')
            return false;
        if (States[num][k] == '0' && num_str[k] != '0')
            return false;
    }
    return true;
}

// is the words possible the descending sequence starts from "num"?
bool is_seq_from(vector<int> &good, const vector<string> &words, int index, int num)
{
    if (index >= words.size())
        return true;
    if (is_possible_num(good, words[index], num))
        return is_seq_from(good, words, index + 1, (num + 9) % 10);
    return false;
}

void work_single(istream &input, ostream &output)
{
    int N;
    input >> N;
    vector<string> words(N);
    for (int i = 0; i < N; i++)
        input >> words[i];

    vector<int> good(7, 0); // initial is unknown
    for (int i = 0; i < 7; i++)
    {
        for (int j = 0; j < words.size(); j++)
        {
            if (words[j][i] == '1')
            {
                good[i] = 1; // sure good
                break;
            }
        }
    }

    vector<int> possible_starts;
    for (int i = 0; i < 10; i++)
    {
        if (is_seq_from(good, words, 0, i))
            possible_starts.push_back(i);
    }

    set<string> results;
    // NOTE: multiple starts may have the same results due to broken LEDs
    for (auto start : possible_starts)
    {
        auto good_copy = good; // copy
        int num = start;
        // get bad bits
        for (int j = 0; j < words.size(); j++)
        {
            for (int k = 0; k < 7; k++)
            {
                if (words[j][k] != '1' && States[num][k] == '1')
                    good_copy[k] = -1; // sure broken
            }
            num = (num + 9) % 10; // decrease
        }
        string result = States[(start - (words.size() % 10) + 10) % 10];
        for (int k = 0; k < 7; k++)
        {
            if (good_copy[k] < 0) // broken
                result[k] = '0';
            else if (good_copy[k] == 0) // unknown whether broken or not
            {
                if (result[k] == '1')
                {
                    output << "ERROR!" << endl;
                    return;
                }
            }
        }
        results.insert(result);
    }
    if (results.size() == 1)
        output << *results.begin() << endl;
    else
        output << "ERROR!" << endl;
}

void work(istream &input, ostream &output)
{
    int case_count;
    input >> case_count;
    for (int i = 0; i < case_count; i++)
    {
        output << "Case #" << i + 1 << ": ";
        work_single(input, output);
    }
}

int main(int argc, char *argv[])
{
    bool use_stdio = false;
    if (use_stdio)
        work(cin, cout);
    else
    {
        ifstream input("A-large-practice.in");
        ofstream output("A-large-practice.out");
        work(input, output);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值