Question:
There are n different binary numbers (leading 0 is allowed) with the length of M. Someone else has already figured out a number of W in N numbers. You have to guess this number.
Each time, you can only query whether the K number is 1.
When you use the optimal query policy, you need to query the minimum number of times to ensure that you can guess.
For example, if there are two numbers: 1100 and 0110, you only need to ask whether the first or third digit is 1 to guess. Therefore, the answer is 1.
Analysis:
D (s, A) indicates the set S that has been queried. In the set that has been queried, the set where W is 1 is a. How many times do you need to ask.
If the K-digit number of the next query is:
Then, take the minimum value of all K values.
Preprocessing:
For each S and A, you can first record the number of numbers that meet the conditions and save them in CNT [s] [.
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 const int maxm = 12, maxn = 130; 7 char object[maxn][maxm + 10]; 8 int d[1<<maxm][1<<maxm], cnt[1<<maxm][1<<maxm], vis[1<<maxm][1<<maxm]; 9 int m, n, kase = 0;10 11 int dp(int s, int a)12 {13 if(cnt[s][a] <= 1) return 0;14 if(cnt[s][a] == 2) return 1;15 16 int& ans = d[s][a];17 if(vis[s][a] == kase) return ans;18 vis[s][a] = kase;19 20 ans = m;21 for(int k = 0; k < m; ++k)22 {23 if(!(s&(1<<k)))24 {25 int s2 = s | (1<<k), a2 = a | (1<<k);26 if(cnt[s2][a] >= 1 && cnt[s2][a2] >= 1)27 {28 int need = max(dp(s2, a2), dp(s2, a)) + 1;29 ans = min(ans, need);30 }31 }32 }33 return ans;34 }35 36 int main()37 {38 //freopen("in.txt", "r", stdin);39 while(scanf("%d%d", &m, &n) && n)40 {41 ++kase;42 for(int i = 0; i < n; ++i)43 scanf("%s", object[i]);44 memset(vis, 0, sizeof(vis));45 memset(cnt, 0, sizeof(cnt));46 for(int i = 0; i < n; ++i)47 {48 int feature = 0;49 for(int f = 0; f < m; ++f)50 if(object[i][f] == ‘1‘) feature |= (1 << f);51 for(int s = 0; s < (1<<m); ++s)52 cnt[s][s&feature]++;53 }54 printf("%d\n", dp(0, 0));55 }56 57 return 0;58 }
Code Jun
Ultraviolet A 1252 (shape-pressure DP + memory-based search) Twenty Questions