UVa 1252 Twenty Questions
2015-06-02 23:10
441 查看
Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with ``yes" or ``no". Using those features, we can identify any object fromthe rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features.He/she immediately answers each question with ``yes" or ``no" correctly. You can choose the next question after you get the answer to the previous question.You kindly pay the answerer 100 yen as a tip for each question. Because you don't have surplus money, it is necessary to minimize the number of questions in the worst case. You don't know what is thecorrect answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the numberof features, and the number of objects, respectively. You can assume 0 < m11 and 0< n128. It is followed by n lines, each of which corresponds to an object. Eachline includes a binary string of length m which represent the value (``yes" or ``no") of features. There are no two identical objects.The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.Sample Input
8 1 11010101 11 4 00111001100 01001101011 01010000011 01100110001 11 16 01000101111 01011000000 01011111001 01101101001 01110010111 01110100111 10000001010 10010001000 10010110100 10100010100 10101010110 10110100010 11001010011 11011001001 11111000111 11111011101 11 12 10000000000 01000000000 00100000000 00010000000 00001000000 00000100000 00000010000 00000001000 00000000100 00000000010 00000000001 00000000000 9 32 001000000 000100000 000010000 000001000 000000100 000000010 000000001 000000000 011000000 010100000 010010000 010001000 010000100 010000010 010000001 010000000 101000000 100100000 100010000 100001000 100000100 100000010 100000001 100000000 111000000 110100000 110010000 110001000 110000100 110000010 110000001 110000000 0 0
Sample Output
0 2 4 11 9
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cmath>using namespace std;// 问题数目,对象数目int num_q, num_obj;// 每个对象的特征// array[i]的第j位为1代表第i个对象具有j特征int array[150];// cnt[s][a]代表询问s特征集合,满足a特征集合(不满足s-a特征集合)的对象个数int cnt[2200][2200];// record[s][a]代表已询问s特征集合,目标对象满足a特征集合(不满足s-a特征集合)的最少问题个数int record[2200][2200];int get_min(int s, int a);int main(){while((cin >> num_q >> num_obj) && !(num_q == 0 && num_obj == 0)){memset(record, -1, sizeof(record));memset(cnt, 0, sizeof(cnt));// 读入情况for(int i = 1; i <= num_obj; i++){string s;cin >> s;int num = 0;for(int j = 0; j < s.length(); j++)num = num*2 + (s[j]-'0');array[i] = num;// cout << "s: " << s << " array: " << array[i] << endl;}// 计算cntint end = pow(2, num_q)-1;for(int s = 0; s <= end; s++){for(int i = 1; i <= num_obj; i++){int a = s & array[i];cnt[s][a]++;}}// 计算结果printf("%d\n", get_min(0, 0));}return 0;}// 计算已询问s特征集合,目标对象满足a特征集合(不满足s-a特征集合)的最少问题个数结果int get_min(int s, int a){if(record[s][a] != -1)return record[s][a];if(cnt[s][a] == 1){record[s][a] = 0;return record[s][a];}// 检查所有可行的继续问的特征record[s][a] = (1<<30);for(int i = 1; i <= num_q; i++){int this_s = (1 << (num_q-i));if((this_s & s) == 0){int r1 = get_min(s+this_s, a) + 1;int r2 = get_min(s+this_s, a+this_s) + 1;int r = max(r1, r2);if(r < record[s][a])record[s][a] = r;}}return record[s][a];}
这题一开始想是构造状态d(c, s)代表选了问题集合c以后,现在需要分的对象集合s,该情况下还需要问的最少问题。
后来发现s可以达到2^128,无法实现。
看了书,发现的做法是假设一个对象,d(s, a)代表选了问题集合s, 该对象符合问题a集合,该情况下还需要问的最少问题。
另外,这个问题就是构造最优的决策树,动态规划得到的答案复杂度是特征指数级的。构造最优决策树问题上世纪70年代就被证明是NPC了。
所以现在的算法都是基于启发式来贪心(ID3,C4.5等等)。
相关文章推荐
- 用MFC实现WebGUI--(CDHtmlDialog)
- 在js中获取query string 以及重写URL的函数
- [NetTopologySuite](1)线面相交
- CSS3新增属性选择器: [attribute*=value] 、[attribute^=value] 和[attribute$=value]
- DuiLib 最小框架
- Qt Gui程序显示文本用QPlainTextEdit
- iOS积累 002:UIImage的缩放与压缩
- UIView UIButton UIImageView 添加事件
- vector ,deque 和 list
- iOS开发系列--UITableView全面解析
- UILabel自适应高度和自动换行
- iOS_UITextField 基本操作
- Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'yintingting_baisi/Info.plist'.
- Android Volley完全解析(三),定制自己的Request
- iOS_UIButton 简单操作
- iOS按钮UIButton常用属性总结
- UIScrollView代理方法
- easyui换主题,并记录在cookie.以及cookie作用域介绍
- java 的builder模式——设计思想
- .net mvc4 从客户端中检测到有潜在危险的 Request.Form 值