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.
|
|
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.
Author: WANG, Yucheng
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
char S[10][10] =
{
"1001111",
"0010010",
"0000110",
"1001100",
"0100100",
"0100000",
"0001111",
"0000000",
"0000100"
};
char s[10][10];
int p[10];
int n;
vector<LL>sot[1 << 9];
vector<int>vt[1 << 9];
struct A
{
int p;
char str[10];
bool operator < (const A & b)const
{
return p < b.p;
}
}a[10];
LL v[10];
int main()
{
int top = 1 << 9;
for (int sta = 0; sta < top; ++sta)
{
for (int i = 0; i < 9; ++i)if (sta >> i & 1)
{
vt[sta].push_back(i);
}
}
for (int i = 0; i < 7; ++i)p[i] = i;
do
{
for (int i = 0; i < 9; ++i)
{
v[i] = 0; LL pos = 0;
for (int j = 0; j < 7; ++j)
{
v[i] |= ((LL)(S[i][p[j]] - '0') << pos++);
}
}
for (int sta = 0; sta < top; ++sta)//sta表示行的选择状态集
{
LL now = 0; LL pos = 0;
for (auto i : vt[sta]) { now |= v[i] << pos; pos += 7; }//快一点
//for (int i = 0; i < 9; ++i)if (sta >> i & 1) { now |= v[i] << pos; pos += 7; }
sot[sta].push_back(now);
}
} while (next_permutation(p, p + 7));
for (int i = 0; i < top; ++i)
{
sort(sot[i].begin(), sot[i].end());
}
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
scanf("%d", &n);
int sta = 0;
LL now = 0; LL p = 0;
for (int i = 1; i <= n; ++i)scanf("%d%s", &a[i].p, a[i].str);
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i)
{
int x = a[i].p; --x; sta |= 1 << x;
for (int j = 0; j < 7; ++j)
{
now |= ((LL)(a[i].str[j] - '0') << p++);
}
}
auto it = lower_bound(sot[sta].begin(), sot[sta].end(), now);
if (it != sot[sta].end() && *it == now)puts("YES");
else puts("NO");
}
return 0;
}
/*
【trick&&吐槽】
我们要学会观察数据寻求突破,7 * 9 = 63,我们恰好可以用LL表示
【题意】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954
有9行*7列的一个01矩阵。
我们可以选择任意交换若干列(其实就是一个关于列的全排列)。
现在告诉你若干行的信息,问你是否有这样的交换全排列。
【分析】
<1> 先对列做全排列,这里7!
<2> 然后对行的选取做枚举,这里2^9
<3> 把数哈希起来放到一个size为2^9的空间里
<4> 对于每组数据,我们通过二分查找来判定YES or NO
*/