zoj3954 详细讲解 排序比较单词法
2017-08-08 14:26
281 查看
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.
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 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 ≤i ≤n,si
=cp,xi
holds?
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 <j ≤n,xi
≠xj holds for each test case.
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.
|
|
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 |
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 ≤i ≤n,si
=cp,xi
holds?
Input
The first line of the input is an integerT (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 <j ≤n,xi
≠xj 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\n"); else printf("NO\n"); return ; } int main() { int T; scanf("%d",&T); while(T--) _in(); return 0; } //
2017-08-08 14:17:54 | C++ | 120ms | 284kb | fcy6 时间上已经很优了 |
相关文章推荐
- zoj3954 详细讲解 排序比较单词法
- 比较详细的讲解LCD驱动文档。
- 详细讲解Zen Cart和 Magento比较分析
- DES加密程序代码(带有比较详细的讲解)
- 讲解makefile 比较详细
- java实现排序算法之冒泡排序法详细讲解
- 全文索引xunsearch的详细讲解及sphinx的比较(笔记1)
- meanshift算法讲解,比较详细
- 动态规划(4)详细讲解各最短路径算法及比较
- 【郝斌数据结构自学笔记】75-78_链式二叉树遍历具体程序演示_5种常用排序概述和快速排序详细讲解_再次讨论什么是数据结构_再次讨论到底什么是泛型
- 算法—比较两种排序算法:选择排序和插入排序(详细)
- vb Hook比较详细的讲解 ---> vb获取按键消息(钩子方式)
- 动态规划(4)详细讲解各最短路径算法及比较
- 插入排序加二分排序详细讲解(附代码)
- 比较详细的一篇关于autorun.inf的讲解
- HTTP代理如何正确处理Cookie(2) - 转载(写的比较详细,并以实例讲解)
- java三个数排序比较大小的完整代码,并给出详细解释,初学者,谢谢
- Java 动态代理机制分析及扩展_讲解比较详细,收藏
- 讲解DLL内容的比较详细的站点
- (STL中自带的排序功能7.3.3)POJ 1318 Word Amalgamation(求解一个单词是否在字典里面。解法:将单词按字典序处理后的结果与字典中的单词安字典序处理后的结果进行比较)