Algorithm: UVa 1252

Source: Internet
Author: User
Tags min

The main effect of the topic

There are n binary strings of length m, each of which is different.

In order to separate all the strings, you can ask whether a bit is 0 or 1 at a time.

Ask the least number of questions, and you can distinguish all strings.

Ideas

F[S1][S2]: The question is {S1} collection, the answer is {s2}, you need to ask several times before you can separate all areas

When the problem set is {S1}, if you can't distinguish all the answers, you need to continue to ask another question.

Then you can launch the next set of questions:

Nextquestions = {S1 | (1<<k) when the K-bit of the S1 is 0}

Then you can get:

F[S1][S2] = 0, if the same number as the answer s2 is less than or equal to 1, then all can be separated, and ask 0 times

F[S1][S2] = {min (f[nextquestions][s1], f[nextquestions][s1^ (1<<k)), when S1 's K-bit is 0 o'clock}

Code

/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-1252 Twenty Q Uestion * @type: Memory search * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *========== =================================*/#include <iostream> #include <cstdio> #include <algorithm> #
    
include<vector> #include <queue> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
    
Const double PI = ACOs (-1.0);
const int MAXN = 130;
    
int n, m;
int P[MAXN];
int f[(1<<11) +10][(1<<11) +10];
    
www.bianceng.cn int dfs (int s1, int s2) {if (F[S1][S2)!= INF) return F[S1][S2];
int cnt = 0;
    
for (int i = 0; i < n; ++i) if ((P[i] & S1) = = s2) ++cnt;
    
if (CNT <= 1) {f[s1][s2] = 0; return 0;} for (int i=0; i<m; ++i) {if (s1& (1<<i)) continue; int nextquest = S1 |
(1<<i); int& d = f[s1][s2];
d = min (d, Max (Dfs (nextquest, S2), DFS (Nextquest, s2^ (1<<i))) +1);
return F[S1][S2];  int main () {char str[130]; while (~SCANF ("%d%d", &m, &n) && n+m) {for (int i = 0; i < n; ++i) {scanf ("%s", str); p[i] = 0; for (int j = 0; Str[j] ++j) if (str[j] = = ' 1 ') p[i] |= 1<<j;} memset (f, INF, size
Of (f));
printf ("%d\n", DFS (0, 0));
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.