2014鞍山现场赛H题HDU5077(DFS减枝+打表)
来源:互联网 发布:java kafka offset 编辑:程序博客网 时间:2024/05/18 00:39
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?
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.
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
100010011
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秒,感觉挺快的~
0 0
- 2014鞍山现场赛H题HDU5077(DFS减枝+打表)
- hdu 5077 NAND(打表)2014 Asia regional 鞍山站 H题
- 2014 鞍山 现场赛 E题题解
- 2014鞍山现场赛 HDU5071(模拟)
- 2014鞍山现场赛D题 E题||hdu5073、5074
- hdu 5074 DP 2014鞍山现场赛题
- hdu 5073 2014鞍山现场赛题 物理题
- HDU 5073 Galaxy (2014鞍山现场赛D题)
- hdu 5073 Galaxy /2014鞍山现场赛D题
- hdu 5074 Hatsune Miku(2014 鞍山现场赛)
- hdu 5073 Galaxy(2014 鞍山现场赛)
- hdu 5078 Osu! (鞍山现场赛I题)
- hdu 5071 Chat (鞍山现场赛B题)
- hdu 5073 Galaxy (鞍山现场赛D题)
- hdu 5074 Hatsune Miku (鞍山现场赛E题)
- 2014鞍山现场赛C题HDU5072(素筛+容斥原理)
- hdu 5071(2014鞍山现场赛B题,大模拟)
- hdu 5071 Chat /2014鞍山现场赛B题 (模拟)
- Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
- 面试题---2014.3.29阿里巴巴实习生招聘-研发工程师笔试题
- 基于meanshift的手势跟踪与电脑鼠标控制(手势交互系统)
- 企业IT运维综合管理系统(CooCare Enterprise Edition V1.0)白皮书
- 学习安卓开发的兴奋与郁闷
- 2014鞍山现场赛H题HDU5077(DFS减枝+打表)
- CMM软件能力成熟度模型
- Ubuntu 14.04版64位机器上解决 android 的 adb 无法执行的问题
- 南宁哪家医院丰胸整形好【南宁总队】
- oracle 操作数据表结构
- Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
- sort stl
- cocos2dx 导演、场景、层、精灵之間的關係
- Fedora下编译模块驱动