ZOJ-3954-Seven-Segment Display
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.
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.
题目链接:ZOJ-3954
题目大意:如图所示。给你n串,问是否存在一个合理的a~g序列使得合法
题目思路:刚开始用字符串做超时了,后来用int做就能过。
样例3:
给出n串
我们可以得到:
===>10,11,00…数字
根据1,7,取出原串的1,7串:
===>也能得到几个数字:10,00,00,11…数字
如果数字能完全吻合,则YES。
以下是代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <list>
using namespace std;
int s[10];
int ret[10];
int p[10] = {1000000, 100000, 10000, 1000, 100, 10, 1};
int a[10];
int b[10];
void init()
{
s[1] = 1001111;
s[2] = 10010;
s[3] = 110;
s[4] = 1001100;
s[5] = 100100;
s[6] = 100000;
s[7] = 1111;
s[8] = 0;
s[9] = 100;
}
int main()
{
int _;
cin >> _;
init();
while(_--)
{
int n;
scanf("%d",&n);
memset(ret, -1, sizeof ret);
int pos1 = 0, pos2 = 0;
for (int i = 0; i < n; i++)
{
int id;
int num;
scanf("%d%d",&id,&num);
ret[id] = num;
}
for (int i = 0; i < 7; i++)
{
int x = 0, y = 0;
for (int j = 1; j <= 9; j++)
{
if (ret[j] != -1)
{
x = x * 10 + (ret[j] / p[i] % 10);
y = y * 10 + (s[j] / p[i] % 10);
}
}
a[pos1++] = x;
b[pos2++] = y;
}
sort(a, a + 7);
sort(b, b + 7);
int ans = 1;
for (int i = 0; i < 7; i++)
{
if (a[i] != b[i]) ans = 0;
}
if (ans) printf("YES\n");
else printf("NO\n");
}
return 0;
}