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 ato 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.
|
|
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 ≤ i ≤ n, 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 < j ≤ n, xi ≠ 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
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.
題意:給你每個字母對應的位置,0表示亮,1表示不亮,構成數字1~9的情況如下:
|
|
現在重新排列字母,給你重新排列字母后的部分數字及其對應表中的值,問你是否合法。
思路:
把給你的那些數字的第i位都抽出來形成一個串,再把原串對應數字的第i位都抽出來形成一個串,分別排序,然後一一對比是否相等即可。(好好想想爲什麼)
代碼:
#include<bits/stdc++.h>
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=50;
int n,m,k;
int x,y;
struct node
{
int num;
int v;
bool operator<(node aa)const
{
return num<aa.num;
}
}c[maxn];
int a1[maxn],a2[maxn],ct1,ct2;
int a[]={0,1001111,10010,110,1001100,100100,100000,1111,0,100};
int d[]={1,10,100,1000,10000,100000,1000000,10000000};
int main()
{
int T,fg;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
rep(i,0,n-1) scanf("%d%d",&c[i].num,&c[i].v);
sort(c,c+n);
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
rep(i,0,n-1)
rep(j,0,6)
{
if((c[i].v/d[j])&1) a1[j]|=(1<<i);
if((a[c[i].num]/d[j])&1) a2[j]|=(1<<i);
}
fg=1;
sort(a1,a1+7);sort(a2,a2+7);
rep(i,0,6) if(a1[i]!=a2[i]) fg=0;
if(fg) puts("YES");
else puts("NO");
}
return 0;
}