Ultraviolet A 1252 (status compression dp)

Source: Internet
Author: User

Ultraviolet A 1252 (status compression dp)

Question: There are n binary strings whose lengths are both m and they are not the same. To ask at least how many questions can be asked to completely separate these n strings.
Question: 1 <= m <= 11. It can be inferred from this range that it is State compression. Therefore, dp must have a one-dimensional question, then, the other one is to classify the strings Based on the question raised. One is to conform to the state of the question raised, and the other is not. In this way, f [I] [j] indicates the number of questions to be asked when the answer is status j in the I state so that all strings can be separated.
If the same string as answer j under question I is found to have only one or no, it means that f [I] [j] = 0, and no more questions are required, otherwise, you need to ask another question and change the bit of question I from 0 to 1, the answer is to select a greater limit in the values of the dp returned values that change the bit to 1 and that remain unchanged (because the two sides must be separated at the end of the day, only a large number of questions can be selected ).

#include 
  
   #include 
   
    #include 
    
     #include using namespace std;const int INF = 0x3f3f3f3f;const int N = 135;const int M = (1 << 11) + 5;int n, m, a[N], f[M][M];char str[15];int dp(int s1, int s2) {    if (f[s1][s2] != INF)           return f[s1][s2];    int cnt = 0;    for (int i = 0; i < n; i++)        if ((s1 & a[i]) == s2)            cnt++;    if (cnt <= 1)        return f[s1][s2] = 0;    for (int i = 0; i < m; i++) {        if (s1 & (1 << i))            continue;        int temp = s1 | (1 << i);        f[s1][s2] = min(f[s1][s2], max(dp(temp, s2), dp(temp, s2 ^ (1 << i))) + 1);    }    return f[s1][s2];}int main() {    while (scanf(%d%d, &m, &n) == 2 && n + m) {        memset(f, INF, sizeof(f));        for (int i = 0; i < n; i++) {            scanf(%s, str);            a[i] = 0;            for (int j = 0; j < m; j++)                if (str[j] == '1')                    a[i] |= (1 << j);        }        printf(%d, dp(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.