zoj3954 详细讲解 排序比较单词法

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 froma tog, 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 g
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
zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法
zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法
zoj3954 详细讲解 排序比较单词法 zoj3954 详细讲解 排序比较单词法
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 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 permutationp representing numberx ascp,x. For examplecabcdefg,7 = 0001111, andcgbedcfa,7 = 1011010.

Given n seven segment codess1,s2, ... ,sn and the numbersx1,x2, ... ,xn each of them represents, can you find a permutationp, so that for all 1 ≤in,si =cp,xi holds?

Input

The first line of the input is an integer T (1 ≤T ≤ 105), indicating the number of test cases. ThenT test cases follow.

The first line of each test case contains an integern (1 ≤n ≤ 9), indicating the number of seven segment codes.

For the next n lines, thei-th line contains a numberxi (1 ≤xi ≤ 9) and a seven segment codesi (|si| = 7), their meanings are described above.

It is guaranteed that ∀ 1 ≤ i <jn,xixj holds for each test case.

Output

For each test case, output "YES" (without the quotes) if the permutationp 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
   思路如下:
   1:大数据?????一脸懵逼,以每个permutation排序为状态加以判断?不行!
   2:大数据?????估计一下,搜索加剪枝?不行!
   3:大数据,骂题主,准备放弃?不行!
   4:算了,看看样例,发现与未出现的行无关。
   5:可能会想到以y轴方向判断来解决问题。好像可以珜。
  引申:如何判断两个字符串的单个字母的集合(可重复)相同。如s1=“abacc”
s2=“aabcc”。absolutely,用排序:sort(s,s+L)。然后用函数比较,或者单个顺序比较单个字母。
   6,此题就是问已知函数和目标函数是否可以经过排序得到。
   7,x方向排序,以y方向为单位,从上向下连接成字符串或者数字,如y轴=a时,x[1]=1,x[2]=0,x[3]=1.....x[9]=1;
得到字符串或数“101....1”。遇到的题目往往数字过大,而且因为每一位只用了0或1而造成浪费,所以用看成二进制的0或1,此题由于数不大,没有必要,直接上十进制。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<memory.h>
#include<cstring>
#include<algorithm>
using namespace std;
int m[10][8]={0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0};//我自己手动打表,mmp
int num[10];//为了越界最好可以开大一点,不过我为了刷排名,开到了最小
int ans[8];
int st[8];
void _in()
{
	int s,i,j;
	char c[10][8];
	scanf("%d",&s);//换成cin就不能AC ,可以试一试
	for(i=1;i<=s;i++){
	   scanf("%d",&num[i]);
	   for(j=0;j<=7;j++) c[i][j]=getchar();//j=0时是输入的空格
  	     //如果是cin则不必考虑空格
        } 
	for(i=1;i<=7;i++)
	 {
		ans[i]=0;
		for(j=1;j<=s;j++)
	        ans[i]=ans[i]*10+c[j][i]-'0';//c[]保持的字符,需要///减去‘0’或者48
	 }
	 for(i=1;i<=7;i++)
	 { 
		st[i]=0;
		for(j=1;j<=s;j++)
	        st[i]=st[i]*10+m[num[j]][i];
	 }
	sort(ans+1,ans+8);//排序再比较
	sort(st+1,st+8);
	bool temp=true;//也可以用函数直接比较st和ans是否相同,本题小,懒得搞
	for(i=1;i<=7;i++) if(ans[i]!=st[i]) temp=false;
	if(temp) printf("YES
");
	else printf("NO
");
	return ;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--) _in();
	return 0;
} //
2017-08-08 14:17:54 C++ 120ms 284kb fcy6  时间上已经很优了