ZOJ - 3954 -- Seven-Segment Display -- 思维题(好题)

Description

A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays. Seven segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.

The segments of a seven segment display are arranged as a rectangle of two vertical segments on each side with one horizontal segment on the top, middle, and bottom. If we refer the segments as the letters from a to g, it's possible to use the status of the segments which is called a seven segment code to represent a number. A standard combination of the seven segment codes is shown below.

X a b c e f g
1 1 0 0 1 1 1
2 0 0 1 0 1 0
3 0 0 0 1 1 0
4 1 0 0 1 0 0
5 0 1 0 1 0 0
6 0 1 0 0 0 0
7 0 0 0 1 1 1
8 0 0 0 0 0 0
9 0 0 0 1 0 0

A seven segment code of permutation p is a set of seven segment code derived from the standard code by rearranging the bits into the order indicated by p. For example, the seven segment codes of permutation "gbedcfa" which is derived from the standard code by exchanging the bits represented by "a" and "g", and by exchanging the bits represented by "c" and "e", is listed as follows.

X g b e d c f a
1 1 0 1 1 0 1 1
2 0 0 0 0 1 1 0
3 0 0 1 0 0 1 0
4 0 0 1 1 0 0 1
5 0 1 1 0 0 0 0
6 0 1 0 0 0 0 0
7 1 0 1 1 0 1 0
8 0 0 0 0 0 0 0
9 0 0 1 0 0 0 0

We indicate the seven segment code of permutation p representing number x as cp, x. For example cabcdefg,7 = 0001111, and cgbedcfa,7 = 1011010.

Given n seven segment codes s1, s2, ... , sn and the numbers x1, x2, ... , xn each of them represents, can you find a permutation p, so that for all 1 ≤ in, si = cp, xi holds?


Input

The first line of the input is an integer T (1 ≤ T ≤ 105), indicating the number of test cases. Then T test cases follow.

The first line of each test case contains an integer n (1 ≤ n ≤ 9), indicating the number of seven segment codes.

For the next n lines, the i-th line contains a number xi (1 ≤ xi ≤ 9) and a seven segment code si (|si| = 7), their meanings are described above.

It is guaranteed that ∀ 1 ≤ i < jn, xixj holds for each test case.

Output

For each test case, output "YES" (without the quotes) if the permutation p exists. Otherwise output "NO" (without the quotes).

Sample Input
3
9
1 1001111
2 0010010
3 0000110
4 1001100
5 0100100
6 0100000
7 0001111
8 0000000
9 0000100
2
1 1001111
7 1010011
2
7 0101011
1 1101011
Sample Output
YES
NO
YES
Hint

For the first test case, it is a standard combination of the seven segment codes.

For the second test case, we can easily discover that the permutation p does not exist, as three in seven bits are different between the seven segment codes of 1 and 7.

For the third test case, p = agbfced.

 

题意:

  一个晶体管的每个段标号为 abcdefg,那么显示 1 的时候,7个段的数值为 1001111;显示7的时候为 0001111
  如果要改变 abcdefg 的顺序为 gbedcfa时,显示 1 时二进制序列为 1011011;显示7时为 1011010

  那么问题来了。。。

    给出一个组序列,1-9分别用一组二进制表示,在不知道 abcdefg 的顺序的情况下,如何判断该序列能不能成功的显示出1-9的数字

 

思路:

  不论abcdefg的顺序如何改变,比如 1 和 7

1 1 0 0 1 1 1 1
7 0 0 0 1 1 1 1

 

  

  他们中 有 1 个 10,4 个 11 以及两个 00

  当 abcdefg 的顺序改变了的话,如

1 1 1 0 1 0 1 1
7 0 1 0 1 0 1 1

 

  

  他们中 也是存在 1个10,4个11以及 两个00

  按照上边说的,最终我们只要统计出输入的序列中 每个列中 这种组合的的个数与正确的比对一下就好了

  但当在输入的顺序为不是1-9递增的输入呢???(很简单嘛,排序下就好了,用个结构体搞定!!!)代码如下

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define maxn 3010
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
//将正确的做为一个模板,将每次输入的数据与该数据进行比对
int sample[10][8] = {
    {-1,-1,-1,-1,-1,-1,-1},
    {1,0,0,1,1,1,1},
    {0,0,1,0,0,1,0},
    {0,0,0,0,1,1,0},
    {1,0,0,1,1,0,0},
    {0,1,0,0,1,0,0},
    {0,1,0,0,0,0,0},
    {0,0,0,1,1,1,1},
    {0,0,0,0,0,0,0},
    {0,0,0,0,1,0,0}
};
struct Light{
    int index;
    char tmp[8]; 
    bool operator < (const Light &a)const {
        return index < a.index;
    }
};
Light light[10];
int main()
{
    int t;scanf("%d",&t);
    while(t--){
        map<int,int> m1,m2;
        int n;scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d",&light[i].index);
            scanf("%s",&light[i].tmp);
        }
        sort(light+1,light+n+1);
        int flag = false;
        m1.clear();m2.clear();
        for(int i = 0;i < 7;i++){
            int sum1 = 0;
            int sum2 = 0;
            for(int j = 1;j <= n;j++){
                int in = light[j].index;
                sum1 = sum1*10 + light[j].tmp[i]-'0';
                sum2 = sum2*10 + sample[in][i];
            }
            m1[sum1]++;
            m2[sum2]++;
        }
        if(m1 != m2)flag = true;
        if(flag)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

 

posted @ 2017-04-19 08:29  渣渣技术狗  阅读(89)  评论(0)    收藏  举报