Seven-Segment Display(ZOJ - 3954)

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.

X a b c d e f
1 1 0 0 1 1 1 1
2 0 0 1 0 0 1 0
3 0 0 0 0 1 1 0
4 1 0 0 1 1 0 0
5 0 1 0 0 1 0 0
6 0 1 0 0 0 0 0
7 0 0 0 1 1 1 1
8 0 0 0 0 0 0 0
9 0 0 0 0 1 0 0
     
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.

X g b e d c f
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 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 ≤ 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.

补题的时候发现其实不难……主要还是题目太长看不懂,训练的时候被其他的题卡了比较久。也有点心理因素的关系,因为看其他队也没几个做出来的。等训练赛完静下心看其实没有想的那么难,其实就是个水题。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <algorithm>
using namespace std;
//题意大致就是:给定n段code和对应的数字,问这n段code能否组合成给定的排列
int p[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};     //取位数用
int seven[10] = {1011011, 110, 10010, 11001, 110000, 100000, 1011010, 0, 10000};     //题目中给定的code用int类型数组存储
struct number {
    int num, code;
} nd[15];
int main() {
    int t, n;
    scanf("%d", &t);
    while (t--) {
        memset(nd, 0, sizeof nd);
        map <int, int> dic, save;    //dic用来存储输入中组成的排列,save用来存储给定的序列
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%d %d", &nd[i].num, &nd[i].code);
        for (int i = 0; i < 7; i++) {
            int tmp1 = 0, tmp2 = 0, base = 1;       //开始模拟取排序,并将所有排序及数量存入map中
            for (int j = 0; j < n; j++) {
                tmp1 += (nd[j].code / p[i]) % 10 * base;
                tmp2 += (seven[nd[j].num - 1] / p[i]) % 10 * base;
                base *= 10;
            }
            dic[tmp1]++;
            save[tmp2]++;
        }
        bool flag = dic == save ? true : false;     //由题意可知,当所有的序列和数量都相同时一定能组成给定的序列,因此只需要判断dic和save是否相同
        if (flag) printf("YES\n");
        else printf("NO\n");
    }
}

猜你喜欢

转载自blog.csdn.net/white_yasha/article/details/79800215
ZOJ