NAND

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 65    Accepted Submission(s): 14

Problem Description
Xiaoqiang entered the “shortest code” challenge organized by some self-claimed astrologists. He was given a boolean function taking n inputs (in C++):



bool f(bool x1, bool x2, bool x3){

//your code goes here

//return something

}




All possible inputs and expected outputs of this function have been revealed:







Xiaoqiang’s code must be like:



bool a = NAND(b, c);



where “a” is a newly defined variable,“b” and “c” can be a constant (0/1) or a function parameter (x1/x2/x3) or a previously defined variable. NAND is the “not-and” function:



NAND(b, c)=!(b&&c)



Because NAND is universal, Xiaoqiang knew that he could implement any boolean function he liked. Also, at the end of the code there should be a return statement:



return y;



where y can be a constant or a function parameter or a previously defined variable. After staring at the function for a while, Xiaoqiang came up with the answer: 



bool a = NAND(x1, x2);

bool b = NAND(x2, x3);

bool y = NAND(a, b); return y;




Xiaoqiang wants to make sure that his solution is the shortest possible. Can you help him?
 
Input
The first line contains an integer T (T ≤ 20) denoting the number of the test cases.



For each test case, there is one line containing 8 characters encoding the truth table of the function.
 
Output
For each test case, output a single line containing the minimum number of lines Xiaoqiang has to write.
 
Sample Input
1
00010011
 
Sample Output
4

题意:RT

思路:这题简化题意就是,要求构造最少的NAND式子,使得输入x1,x2,x3,输出一个8位二进制数

            因为x1,x2,x3的全部组合满足0~8。那么能够将这三个数的8种值先按列压成3个8位二进制数(类似于搜索的时候开了8个栈,这样压以后仅仅需一个栈。方便处理。减枝)

            x1,x2,x3的取值例如以下
            000
            001
            010
            011
            100
            101
            110
            111
            按列压成8位二进制。x1 : 00001111  x2 : 00110011  x3 : 01010101

            然后不难发现全部的NAND操作变成了~(a&b)

            搜索的时候将新值入栈,假设搜到反复的就直接跳过,这个用一个数组记录每一个数是否存在就好了

            另一个非常重要的减枝是设置一个start变量,由于每次得到新的数是从当前栈里的元素两两进行NAND操作得到的

            而在DFS进入下一层的时候实际上队列中的有些元素已经两两运算过了,所以就不须要再算一次,start的含义是下一层DFS里的循环遍历应该从栈的哪个位置開始

            打完表花了15秒,感觉挺快的~

            

版权声明:本文博客原创文章,博客,未经同意,不得转载。

2014鞍山直播比赛H称号HDU5077(DFS修剪+通过计)的更多相关文章

  1. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

  2. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  3. H - Graphics(dfs)

    H - Graphics   Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submi ...

  4. (比赛)B - 棋盘问题(dfs)

    B - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Practice POJ ...

  5. HDU 5071 Chat(2014鞍山B,模拟)

    http://acm.hdu.edu.cn/showproblem.php?pid=5071 Chat Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. CHD 2014迎新杯比赛题解

    A. 草滩的魔法学校 分析: 高精度乘法 或 JAVA大数类 很明显 10000 的阶乘已经远远超过 64 位数能表示的范围了.所以我们要用一个比较大的数组来存放这个数.那数组要开多少位合适呢?我们不 ...

  7. hdu 5073 Galaxy(2014 鞍山现场赛)

    Galaxy                                                                   Time Limit: 2000/1000 MS (J ...

  8. hdu 5071(2014鞍山现场赛B题,大模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5071 思路:模拟题,没啥可说的,移动的时候需要注意top的变化. #include <iostr ...

  9. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

随机推荐

  1. RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools

    Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...

  2. grep在一个特定的文件搜索文件夹keyword

    grep -R --include="*.*"(文件名匹配)  key(keyword) dir(夹) eg.在当前文件夹搜索xml关键文件172.19.32.22 grep -R ...

  3. Java误区: 静态代码块,当把类将被载入到自己主动运行?

    JAVA静态代码块会在类被载入时自己主动运行? 非常多Java开发人员的思想,被这个思想深深的轮奸了n遍,传播这个错误思想的博客,在网上一堆,越来越多的人被轮奸. 如:http://blog.csdn ...

  4. OFTP说明

    OFTP (TheOdette File Transfer Protocol,RFC 2204)作为两个商业伙伴中建立EDI连接的一种协议.它由Odette-Organization于1986年创建. ...

  5. 如何在Dreamweaver中使用zen coding

    在我发表上一篇<Zen Coding: 一种快速编写HTML/CSS代码的方法>之后,有网友表示不知道怎么在Dreamweaver上使用zen coding插件.OK,今天我就写一篇详细的 ...

  6. 使用Bootstrap 他写道网站

    新发现vdceye有些难看的网站,为了找到一个bootstrap工具,我一次又一次地写信给网站 这个工具是很容易使用 http://vdceye.com/ 版权声明:本文博客原创文章,博客,未经同意, ...

  7. Oracle中merge into的使用 (转)

    该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关键词,ORACLE 10g 做了如下改动. 1.ins ...

  8. 关于苹果公司最新的语言Swift

    Swift供IOS和OSX新的编程语言开发的应用程序,吸取C和Objective-C质朴的语言.但没有损失C兼容性语言.Swift使用安全的编程模型.增加各种现代编程语言功能,使语言更容易掌握.更具可 ...

  9. jquery之ajax中国乱码的解决方案

    $.ajax({ dataType : 'json',type : 'POST',url : 'http://localhost/test/test.do',data : {id: 1, type: ...

  10. Unity3d C# Socket 下载文件 (同步到)

    续篇 Unity3d C# HttpWebRequest 异步下载文件 ,由于project编译为IL2CPP的情况下仍然无效.提示HttpWebrequest 在当前版本号不支持.所以还是寻求其他的 ...