ZOJ

来源:互联网 发布:巨化集团待遇知乎 编辑:程序博客网 时间:2024/06/05 15:25

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

题意:自定义一个计时器的每段的位置,看能否使其亮起来正好是给出的数字。

思路;因为位置自定义的是不确定的,但是我们知道当一个数亮时,另一个数在此位置上是亮还是不亮,如果两个自定义的序列亮相同的数字每列的元素是一样的,例如样例三,之前定义的1,7每列是10,00,00,11,11,11,11,重新定义之后是10,00,00,11,11,11,11,两者虽然书位置不一样但是所组成的数是一样的,所以我们只需要判断每列组成元素是否和之前的一样就行了。

代码:

#include <iostream>#include<stdio.h>#include<cstdio>#include<iostream>#include<algorithm>#include<math.h>#include<string.h>#include<map>#include<queue>#include<vector>#include<deque>#define ll long long#define inf 0x3f3f3f3f#define mem(a,b) memset(a,b,sizeof(a))using namespace std;int p[10] = {0,1000000, 100000, 10000, 1000, 100, 10, 1};int a[11],b[11],aa[11],bb[11];int vis[10];void init(){    b[1] = 1001111;    b[2] = 10010;    b[3] = 110;    b[4] = 1001100;    b[5] = 100100;    b[6] = 100000;    b[7] = 1111;    b[8] = 0;    b[9] = 100;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        init();        int n,m,num;        scanf("%d",&n);mem(vis,0);        mem(aa,0);mem(bb,0);        for(int i=1; i<=n; i++)        {            scanf("%d%d",&m,&num);            a[m]=num;            vis[m]=1;        }        int k=0,k1=0;        for(int i=1; i<=7; i++)        {            int x=0,y=0;            for(int j=1; j<=9; j++)            {                if(vis[j])                {                    x=x*10+(a[j]/p[i]%10);                    y=y*10+(b[j]/p[i]%10);                }            }            aa[k++]=x;            bb[k1++]=y;        }        sort(aa,aa+k);        sort(bb,bb+k1);        int flag=0;        for(int i=0; i<k; i++)        {            if(aa[i]!=bb[i])            {                flag=1;                break;            }        }        if(flag)printf("NO\n");        else printf("YES\n");    }}


0 0
原创粉丝点击