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;}