【算法竞赛入门经典】集合的动态规划;时间优化 例题9-16 UVa1252

【算法竞赛入门经典】集合的动态规划;时间优化 例题9-16 UVa1252

例题UVa1210817

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 from the 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 the correct 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 number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line 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

分析

这道题目和上一题 9-15 有不少相似之处。
当你问完数个问题之后(问的问题是那些i中bit为1的问题),得知了现在的状态是a(有特征则对应位置bit为1),此时,挑选一个问题再问,问出是否具有这个状态然后分具有和不具有两种情况转移。为了保证每一个都能清楚知道是什么,要取max。再这种情况下,该状态没问的问题都问一次然后挑一个最小的即可。因此,d[s][a]的含义是,在问问题的状态为s,得知了特征状态是a的情况下,还需要问多少次。
状态转移为:d[s][a]=min{max(dp(s2,a2),dp(s2,a))+1};
此题目需要进行时间优化,事实上优化完了之后也比较好想。
因为每次面对一个d[s][a]的时候,你不知道能不能确定。所以要实现统计,在问问题的状态为s的情况下,得知了特征状态是a的有几个,若是只有一个,那么不用问了,若是有两个,问一次就够了,否则需要按照上一段的步骤再分。若是不事先统计,每次都要去扫描一次判断有几个,那显然效率大大降低,我们在init()中完成这个工作,即:当问问题状态为s的情况下,得知特征状态为a的物体有cnt[s][a]个。

样例实现代码

#include<iostream>
#include<algorithm>
#include<cstring>
#define maxn 128+10
#define maxm 12
using namespace std;
char obj[maxn][maxm];
int cnt[1<<maxm][1<<maxm],d[1<<maxm][1<<maxm];
int m,n;
void init(){
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++){
        int feature=0;
        for(int j=0;j<m;j++){
            if(obj[i][j]=='1'){
                feature|=(1<<j);
            }
        }
        for(int i=0;i<(1<<m);i++){
            cnt[i][i&feature]++;
        }
    }
}
int dp(int s,int a){
    if(cnt[s][a]==1)
        return 0;
    else if(cnt[s][a]==2)
        return 1;
    int &ans=d[s][a];
    if(ans>=0)
        return ans;
    ans=m;
    for(int i=0;i<m;i++){
        if(!(s&(1<<i))){
            int s2=s|(1<<i);
            int a2=a|(1<<i);
            if(cnt[s2][a2]>0&&cnt[s2][a]>0){
                int need=max(dp(s2,a2),dp(s2,a))+1;
                ans=min(ans,need);
            }
        }
    }
    return ans;
}
int main(){
    while(cin>>m>>n&&m){
        for(int i=0;i<n;i++){
            cin>>obj[i];
        }
        init();
        memset(d,-1,sizeof(d));
        int ans=dp(0,0);
        cout<<ans<<endl;
    }
    return 0;
} 

结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值