Twenty Questions

https://odzkskevi.qnssl.com/15b7eb4cd1f75f63cee3945b0b845e4f?v=1508411736

【题解】

dp[S1][S2]表示已经询问了S1,确定有S2,还需要至少多少次询问

dp[S1][S2] = max(min(dp[S1 | k][S2 | k], dp[S1 | k, S2 | j)) + 1

即枚举询问哪一个k,得到答复是有/没有中取还需要的询问次数最大的,然后

在所有的k中找最小的,表示到了S1,S2这个状态我要去询问k

预处理cnt[S1][S2]表示S2是S1的子集且属性为S2的物体的个数

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) inline void swap(int &a, int &b)
{
int tmp = a;a = b;b = tmp;
} inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXM = + ; int ma,dp[ << MAXM][ << MAXM], cnt[ << MAXM][ << MAXM], n, m, num[MAXN];
char s[MAXM];
/*
dp[s1][s2]表示已经询问了s1,确定有s2,还需要询问多少次
*/
int DP(int s1, int s2)
{
if(cnt[s1][s2] <= )return ;
if(dp[s1][s2] > -)return dp[s1][s2];
dp[s1][s2] = INF;
for(register int i = ;i < m;++ i)
{
if(s1 & ( << i))continue;
dp[s1][s2] = min(dp[s1][s2], max(DP(s1 | ( << i), s2), DP(s1 | ( << i), s2 | ( << i))) + );
}
return dp[s1][s2];
} int main()
{
while(scanf("%d %d", &m, &n) != EOF && m + n)
{
memset(num, , sizeof(num));
memset(dp, -, sizeof(dp));
memset(cnt, , sizeof(cnt));
for(register int i = ;i <= n;++ i)
{
scanf("%s", s);
for(register int j = ;j < m;++ j)
if(s[j] == '') num[i] |= ( << j);
}
ma = << m;
for(register int s1 = ;s1 < ma - ;++ s1)
for(register int i = ;i <= n;++ i)
++ cnt[s1][s1 & num[i]];
printf("%d\n", DP(,));
}
return ;
}

UVA1252

Uva1252 Twenty Questions的更多相关文章

  1. UVA-1252 Twenty Questions (状压DP)

    题目大意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同)最小需要多少次询问? 题目分析:定义dp(s,a ...

  2. UVA 1252 十五 Twenty Questions

    十五 Twenty Questions Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  3. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

  4. UVA1252 【Twenty Questions】

    分析 为了叙述方便,设"心里想的物体"为W.首先在读入时把每个物体转化为一个二进制整数.不难发现,同一个特征不需要问两遍,所以可以用一个集合s表示已经询问的特征集. 在这个集合s中 ...

  5. UVa 1252 (状压DP + 记忆化搜索) Twenty Questions

    题意: 有n个长为m的各不相同的二进制数(允许存在前导0),别人已经事先想好n个数中的一个数W,你要猜出这个数. 每次只可以询问该数的第K为是否为1. 问采用最优询问策略,则最少需要询问多少次能保证猜 ...

  6. Twenty Questions

    题意: 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可以问某位上是0还是1. 问最少提问次数,可以把所有字符串区分开来. 分析: dp[s1][s2]: 表示提 ...

  7. UVa 1252 Twenty Questions (状压DP+记忆化搜索)

    题意:有n件物品,每件物品有m个特征,可以对特征进行询问,询问的结果是得知某个物体是否含有该特征,要把所有的物品区分出来(n个物品的特征都互不相同), 最小需要多少次询问? 析:我们假设心中想的那个物 ...

  8. UVa 1252 - Twenty Questions(记忆化搜索,状态压缩dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可 ...

  9. UVa 1252 - Twenty Questions(状压DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. c语言学习笔记 - 指针和数组

    结合内存存储数据的机制,c语言里指针的出现和使用也就不奇怪了,如果先学了内存的一些知识,以及程序运行机制,到了c指针这块就会清晰很多. #include <stdio.h> int mai ...

  2. 容器安全与EDR的异同

    以Docker为代表的容器技术,直接运行于宿主机操作系统内核,因此对于容器安全,很多人会有着这样的疑问:EDR(Endpoint Detection and Response)等主机安全方案,能否直接 ...

  3. Spring框架使用ByName自动注入同名问题剖析

    问题描述   我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...

  4. Everything-启用http服务器(公网IP)会导致共享文件被搜索引擎搜索

    1. 漏洞利用成功的前提 公网ip 启用http服务器 2.产生原因 启用http服务器功能点:让用户在本地或局域网上的其他电脑使用浏览器进行搜索,并支持文件下载.若拥有公网IP的用户启用此功能,就是 ...

  5. freemarker 嵌套循环 (导出word时,修改ftl模板)

    1.循环 (循环输出reportList列表的每行的姓名) <#list reportList as report> ${report.name} </$list> 2.嵌套循 ...

  6. python格式化输出%,while else

    一.格式化输出% 需求:自我介绍模板 #格式化输出 # % s d %占位符,占个位置 s(字符串),d(数字)替换内容的类型 name = input('请输入姓名:') age = input(' ...

  7. js中控制流管理的四种方法

    引自http://es6.ruanyifeng.com/#docs/generator#yield--表达式 1.常用的回调方法 step1(function (value1) { step2(val ...

  8. Shield 安装与配置

    Shield 安装与配置   https://www.elastic.co/guide/en/shield/shield-1.3/introduction.html  一.简介 Shield是Elas ...

  9. hive新加入字段插入数据需要注意事项

    hive中新加字段需要注意如下 1)如果表中有分区字段,必须先删除分区才能插入数据否则为null; 2)insert override TABLE table1 select counm1,counm ...

  10. Windows API 第17篇 GetLogicalDriveStrings 获取本机所有逻辑驱动器,以根目录的形式表示

    函数原型:DWORD GetLogicalDriveStrings(  DWORD nBufferLength,  // size of buffer                          ...