UVA 1252 (State compression DP)

Source: Internet
Author: User

Test instructions: There are n binary strings, the length is m and all are not the same, ask at least how many questions can be divided into a complete number of n string.
The problem: 1<=m<=11, from this range can be inferred is the state compression, then DP must have one dimension to ask questions, and then another is based on the proposed problem of the string classification, one is in line with the state of the proposed problem, the other is not in accordance with. This f[i][j] indicates how many questions to ask when the answer is state j in question I, in order to separate all the strings.
If found in question I and Answer j the same string only 1 string or not, the description f[i][j]=0 do not need to ask questions to have been differentiated, otherwise you will ask questions, the previous question I is 0 to 1, the answer in the bit 1 and remain unchanged DP return value to choose a larger value ( Because the two sides to distinguish between the open, choose the number of questions can be more.

#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>using namespace STD;Const intINF =0x3f3f3f3f;Const intN =135;Const intM = (1<< One) +5;intN, M, A[n], f[m][m];Charstr[ the];intdpintS1,intS2) {if(F[S1][S2]! = INF)returnF[S1][S2];intCNT =0; for(inti =0; I < n; i++)if((S1 & a[i]) = = s2) cnt++;if(CNT <=1)returnF[S1][S2] =0; for(inti =0; I < m; i++) {if(S1 & (1<< i))Continue;inttemp = S1 | (1<< i); F[S1][S2] = min (F[s1][s2], Max (DP (temp, S2), DP (temp, s2 ^ (1<< i)) +1); }returnF[S1][S2];}intMain () { while(scanf("%d%d", &m, &n) = =2&& n + m) {memset(F, INF,sizeof(f)); for(inti =0; I < n; i++) {scanf('%s ', str); A[i] =0; for(intj =0; J < M; J + +)if(Str[j] = =' 1 ') A[i] |= (1<< j); }printf("%d\n", DP (0,0)); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 1252 (State compression DP)

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.