Temporary

【UVa1252】

 1 // UVa1252 Twenty Questions
 2 // Rujia Liu
 3 #include<cstdio>
 4 #include<ctime>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cassert>
 8 using namespace std;
 9 
10 const int maxn = 128;
11 const int maxm = 11;
12 
13 int kase, n, m;
14 char objects[maxn][maxm + 100];
15 
16 int vis[1<<maxm][1<<maxm], d[1<<maxm][1<<maxm];
17 int cnt[1<<maxm][1<<maxm]; // cnt[s][a]: how many object satisfies: Intersect(featureSet(i), s) = a
18 
19 // s: the set of features we already asked
20 // a: subset of s that the object has
21 int dp(int s, int a) {
22   if(cnt[s][a] <= 1) return 0;
23   if(cnt[s][a] == 2) return 1;
24 
25   int& ans = d[s][a];
26   if(vis[s][a] == kase) return ans;
27   vis[s][a] = kase;
28 
29   ans = m;
30   for(int k = 0; k < m; k++)
31     if(!(s & (1<<k))) { // haven't asked
32       int s2 = s|(1<<k), a2 = a|(1<<k);
33       if(cnt[s2][a2] >= 1 && cnt[s2][a] >= 1) {
34         int need = max(dp(s2, a2),     // the object has feature k
35                        dp(s2, a)) + 1; // the object doesn't have feature k
36         ans = min(ans, need);
37       }
38     }
39   return ans;
40 }
41 
42 void init() {
43   for(int s = 0; s < (1<<m); s++) {
44     for(int a = s; a; a = (a-1)&s)
45       cnt[s][a] = 0;
46     cnt[s][0] = 0;
47   }
48   for(int i = 0; i < n; i++) {
49     int features = 0;
50     for(int f = 0; f < m; f++)
51       if(objects[i][f] == '1') features |= (1<<f);
52     for(int s = 0; s < (1<<m); s++)
53       cnt[s][s & features]++;
54   }
55 }
56 
57 
58 int main() {
59   memset(vis, 0, sizeof(vis));
60   while(scanf("%d%d", &m, &n) == 2 && n) {
61     ++kase;
62     for(int i = 0; i < n; i++) scanf("%s", objects[i]);
63     init();
64     printf("%d\n", dp(0, 0));
65   }
66   return 0;
67 }

 

posted @ 2018-04-16 19:10  Ctfes  阅读(417)  评论(0编辑  收藏  举报