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

来源:互联网 发布:白金数据谁杀的人 编辑:程序博客网 时间:2024/06/07 04:37
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.

Xabcdefg 1100111120010010300001104100110050100100601000007000111180000000900001000 = on1 = 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.

Xgbedcfa 110110112000011030010010400110015011000060100000710110108000000090010000

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

391 10011112 00100103 00001104 10011005 01001006 01000007 00011118 00000009 000010021 10011117 101001127 01010111 1101011

Sample Output

YESNOYES
   思路如下:
   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};//我自己手动打表,mmpint 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\n");else printf("NO\n");return ;}int main(){int T;scanf("%d",&T);while(T--) _in();return 0;} //2017-08-08 14:17:54C++120ms284kbfcy6  时间上已经很优了

 
原创粉丝点击