ZOJ 3954 Seven-Segment Display(思维)

来源:互联网 发布:孤独小说家 知乎 编辑:程序博客网 时间:2024/06/01 07:47

Seven-Segment Display

Time Limit: 1 Second      Memory Limit: 65536 KB

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.

Xabcdefg110011112001001030000110410011005010010060100000700011118000000090000100   0 = on  1 = off

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.

Xgbedcfa110110112000011030010010400110015011000060100000710110108000000090010000

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

Given n seven segment codes s1s2, ... , sn and the numbers x1x2, ... , xn each of them represents, can you find a permutation p, so that for all 1 ≤ i ≤ nsi = cpxi 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 < j ≤ nxi ≠ xj 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

391 10011112 00100103 00001104 10011005 01001006 01000007 00011118 00000009 000010021 10011117 101001127 01010111 1101011

Sample Output

YESNOYES

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.



思路:如果给出的条件能满足,那么就看条件中的行,每列对应的选中行形成一个二进制串,可以用int存储,这样就


能根据给的条件得到一个包含7个int的集合,再去看原序列中对应行形成的7个int的集合。因为只能列交换,所以每列


和给定行形成的int不会变,只是7个int的顺序会发生变化,所以只要判断这两个集合是否相等就可以了,只有这两个


合相等才能满足。


可以参考有图解的点击打开链接


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<map>using namespace std;int v[10][10];struct node{    int row;    char str[10];}a[10];int main(void){    v[1][1] = v[1][4] = v[2][5] = v[2][6] = 1;    v[3][2] = v[4][1] = v[4][4] = v[4][7] = 1;    v[5][1] = v[5][3] = v[5][4] = v[5][5] = 1;    v[5][7] = v[5][9] = v[6][1] = v[6][2] = 1;    v[6][3] = v[6][7] = v[7][1] = v[7][7] = 1;    int n, t;    cin >> t;    while(t--)    {        map<int, int> m1, m2;        m1.clear(), m2.clear();        scanf("%d", &n);        for(int i = 0; i < n; i++)            scanf("%d %s", &a[i].row, a[i].str);        for(int i = 0; i < 7; i++)        {            int tmp1 = 0, tmp2 = 0;            for(int j = 0; j < n; j++)            {                tmp1 = tmp1*2+v[i+1][a[j].row];                tmp2 = tmp2*2+a[j].str[i]-'0';            }            m1[tmp1]++;            m2[tmp2]++;        }        puts(m1==m2 ? "YES" : "NO");    }    return 0;}


0 0
原创粉丝点击