Twenty Questions
题意:
有n个长度为m的二进制串,每个都是不同的。
为了把所有字符串区分开,你可以询问,每次可以问某位上是0还是1。
问最少提问次数,可以把所有字符串区分开来。
分析:
dp[s1][s2]: 表示提问的问题是s1集合,答案是s2时,还需要问几次才可以全部区分开
当问题集合为{s1}时, 如果还不能区分所有答案,那么就需要继续再问一个问题, { s1 | (1<<j), 当s1的j位上为0的时候 }
如果和答案s2相同的个数小于等于1,那么已经可以全部区分开了
如果还没区分开继续下一个问题,因为再最坏的情况下,要取答案s2的两种情况最大值
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int n,m,dp[<<][<<];
int cas[];
int dfs(int s1,int s2){
if(dp[s1][s2]>=)return dp[s1][s2];
dp[s1][s2]=INF;
int num=;
for(int i=;i<n;++i)
if((s1&cas[i])==s2)num++;
if(num<=)return dp[s1][s2]=;
for(int i=;i<m;++i)
if((s1&(<<i))==){
dp[s1][s2]=min(dp[s1][s2],max(dfs((s1|(<<i)),s2),dfs((s1|(<<i)),(s2|(<<i))))+);
}
return dp[s1][s2];
}
int main()
{
char str[];
while(~scanf("%d%d",&m,&n)){
if(m==&&n==)break;
memset(cas,,sizeof(cas));
memset(dp,-,sizeof(dp));
for(int i=;i<n;++i)
{
scanf("%s",str);
for(int j=;j<m;++j)
if(str[j]=='')
cas[i]|=(<<j);
}
printf("%d\n",dfs(,));
}
return ;
}
Twenty Questions的更多相关文章
- UVA 1252 十五 Twenty Questions
十五 Twenty Questions Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
- Uva1252 Twenty Questions
Twenty Questions https://odzkskevi.qnssl.com/15b7eb4cd1f75f63cee3945b0b845e4f?v=1508411736 [题解] dp[S ...
- UVa 1252 (状压DP + 记忆化搜索) Twenty Questions
题意: 有n个长为m的各不相同的二进制数(允许存在前导0),别人已经事先想好n个数中的一个数W,你要猜出这个数. 每次只可以询问该数的第K为是否为1. 问采用最优询问策略,则最少需要询问多少次能保证猜 ...
- UVa 1252 Twenty Questions (状压DP+记忆化搜索)
题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...
- UVa 1252 - Twenty Questions(记忆化搜索,状态压缩dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可 ...
- UVA-1252 Twenty Questions (状压DP)
题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问? 题目分析:定义dp(s,a ...
- UVA1252 【Twenty Questions】
分析 为了叙述方便,设"心里想的物体"为W.首先在读入时把每个物体转化为一个二进制整数.不难发现,同一个特征不需要问两遍,所以可以用一个集合s表示已经询问的特征集. 在这个集合s中 ...
- UVa 1252 - Twenty Questions(状压DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- Oracle 体系结构2 - 实例和数据库
Oracle最最基本的概念: 实例和数据库 实例就是oracle进程和一块共享内存, 数据库就是静态的文件,如datafile, log file, redo logfile, control fil ...
- nohup 程序名 & (使程序推到后台运行,即使终端关闭,该程序依然运行)
IshallbeThatIshallbe:~ iamthat$ ps -ef |grep ping 502 450 1 0 9:30PM ?? 0:00.05 ping www.baidu.com 5 ...
- 什么是spring?
一.对spring的理解. 1.Spring是实现了工厂模式的工厂类(什么是工厂类?简单的来说就是把需要的接口定义到一个类中,需要的时候不用新建,直接从这个类中调用该接口就可以了), 这个类的名字为B ...
- Using the Repository Pattern with ASP.NET MVC and Entity Framework
原文:http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and ...
- java服务器知识
http://blog.csdn.net/chenyi8888/article/details/4484641 http://blog.csdn.net/chenyi8888/article/deta ...
- Protected Member Access
https://msdn.microsoft.com/en-us/library/bcd5672a.aspx 官方的说法The protected keyword is a member access ...
- Android Material Design-TabLayout的使用
TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...
- ASP.NET 数据绑定常用代码及其性能分析
用DataBinder.eval 绑定不必关心数据来源(Dataread或dataset).不必关心数据的类型eval会把这个数据对象转换为一个字符串.在底层绑定做了很多工作,使用了反射性能.正因为使 ...
- 1176. Hyperchannels(欧拉回路)
1176 给定一有向图 求其反图的欧拉回路 路径输反了 一直WA.. #include <iostream> #include<cstdio> #include<cstr ...
- Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...