UVa 1252 Twenty Questions

Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with ``yes" or ``no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.

You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with ``yes" or ``no" correctly. You can choose the next question after you get the answer to the previous question.

You kindly pay the answerer 100 yen as a tip for each question. Because you don't have surplus money, it is necessary to minimize the number of questions in the worst case. You don't know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.

The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.

Input 

The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m$ \le$11 and 0 < n$ \le$128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value (``yes" or ``no") of features. There are no two identical objects.

The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.

Output 

For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.

Sample Input 

8 1 
11010101 
11 4 
00111001100 
01001101011 
01010000011 
01100110001 
11 16 
01000101111 
01011000000 
01011111001 
01101101001 
01110010111 
01110100111 
10000001010 
10010001000 
10010110100 
10100010100 
10101010110 
10110100010 
11001010011 
11011001001 
11111000111 
11111011101 
11 12 
10000000000 
01000000000 
00100000000 
00010000000 
00001000000 
00000100000 
00000010000 
00000001000 
00000000100 
00000000010 
00000000001 
00000000000 
9 32 
001000000 
000100000 
000010000 
000001000 
000000100 
000000010 
000000001 
000000000 
011000000 
010100000 
010010000 
010001000 
010000100 
010000010 
010000001 
010000000 
101000000 
100100000 
100010000 
100001000 
100000100 
100000010 
100000001 
100000000 
111000000 
110100000 
110010000 
110001000 
110000100 
110000010 
110000001 
110000000 
0 0

Sample Output 

0 
2 
4 
11 
9
 
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
// 问题数目,对象数目
int num_q, num_obj;

// 每个对象的特征
// array[i]的第j位为1代表第i个对象具有j特征
int array[150];


// cnt[s][a]代表询问s特征集合,满足a特征集合(不满足s-a特征集合)的对象个数
int cnt[2200][2200];

// record[s][a]代表已询问s特征集合,目标对象满足a特征集合(不满足s-a特征集合)的最少问题个数
int record[2200][2200];

int get_min(int s, int a);

int main()
{
	while((cin >> num_q >> num_obj) && !(num_q == 0 && num_obj == 0))
	{
		memset(record, -1, sizeof(record));
		memset(cnt, 0, sizeof(cnt));
		// 读入情况		
		for(int i = 1; i <= num_obj; i++)
		{
			string s;
			cin >> s;
			
			int num = 0;
			for(int j = 0; j < s.length(); j++)
				num = num*2 + (s[j]-'0');

			array[i] = num;
//			cout << "s: " << s << " array: " << array[i] << endl;
		}

		// 计算cnt
		int end = pow(2, num_q)-1;
		for(int s = 0; s <= end; s++)
		{
			for(int i = 1; i <= num_obj; i++)
			{
				int a = s & array[i];
				cnt[s][a]++;		
			}	
		}		

		// 计算结果
		printf("%d\n", get_min(0, 0));				
	}		
	return 0;	
}

// 计算已询问s特征集合,目标对象满足a特征集合(不满足s-a特征集合)的最少问题个数结果
int get_min(int s, int a)
{
	if(record[s][a] != -1)
		return record[s][a];

	if(cnt[s][a] == 1)
	{
		record[s][a] = 0;
		return record[s][a];
	}

	// 检查所有可行的继续问的特征
	record[s][a] = (1<<30);
	for(int i = 1; i <= num_q; i++)
	{
		int this_s = (1 << (num_q-i));

		if((this_s & s) == 0)
		{
			int r1 = get_min(s+this_s, a) + 1;
			int r2 = get_min(s+this_s, a+this_s) + 1;
				
			int r = max(r1, r2);

			if(r < record[s][a])
				record[s][a] = r;	
		}	
	}	

	return record[s][a];	
}


这题一开始想是构造状态d(c, s)代表选了问题集合c以后,现在需要分的对象集合s,该情况下还需要问的最少问题。
后来发现s可以达到2^128,无法实现。
看了书,发现的做法是假设一个对象,d(s, a)代表选了问题集合s, 该对象符合问题a集合,该情况下还需要问的最少问题。
 
另外,这个问题就是构造最优的决策树,动态规划得到的答案复杂度是特征指数级的。构造最优决策树问题上世纪70年代就被证明是NPC了。
所以现在的算法都是基于启发式来贪心(ID3,C4.5等等)。
As an AI language model, I don't have personal experiences. However, I can provide you with an article about the party's twenty great experiences. Parties are a great way to let loose and have fun with friends and family. There are so many things you can do at a party that can make it a memorable experience. Here are twenty great experiences that can make any party an unforgettable one. 1. Have a theme: Having a theme for the party can make it more exciting and fun. It can be anything from a 70s disco theme to a Hawaiian luau theme. The possibilities are endless. 2. Decorate the space: Decorating the space can make it more festive and inviting. Balloons, streamers, and banners are just some of the items that can be used to decorate the space. 3. Play games: Playing games can be a great way to break the ice and get everyone involved. Some popular games include charades, Pictionary, and beer pong. 4. Have a photo booth: Setting up a photo booth with fun props can be a great way to capture memories and create some hilarious photos. 5. Have a karaoke session: Karaoke can be a great way to get everyone singing and dancing. It’s a great way to break the ice and get everyone involved. 6. Have a dance-off: A dance-off can be a great way to get everyone moving and grooving. It’s a great way to showcase your dance moves and have some fun. 7. Have a potluck: A potluck can be a great way to get everyone involved in the party planning. Everyone can bring their favorite dish to share with everyone else. 8. Have a piñata: A piñata can be a great way to add some excitement to the party. It’s a great way to get everyone involved and have some fun. 9. Have a movie night: A movie night can be a great way to wind down after a long day. It’s a great way to relax and enjoy some good movies with friends and family. 10. Have a bonfire: A bonfire can be a great way to enjoy the outdoors and roast marshmallows. It’s a great way to relax and enjoy some good company. 11. Have a scavenger hunt: A scavenger hunt can be a great way to get everyone involved and have some fun. It’s a great way to explore the area and find hidden treasures. 12. Have a talent show: A talent show can be a great way to showcase everyone’s talents. It’s a great way to have some fun and see what everyone is capable of. 13. Have a DIY station: A DIY station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. 14. Have a game night: A game night can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 15. Have a costume party: A costume party can be a great way to get everyone involved and have some fun. It’s a great way to dress up and be someone else for a night. 16. Have a cocktail-making contest: A cocktail-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your mixology skills and have some fun. 17. Have a dessert-making contest: A dessert-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your baking skills and have some fun. 18. Have a paint night: A paint night can be a great way to get creative and have some fun. It’s a great way to paint something fun and enjoy some good company. 19. Have a board game tournament: A board game tournament can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 20. Have a DIY cocktail station: A DIY cocktail station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. In conclusion, there are so many great experiences that can make any party an unforgettable one. Whether it’s playing games, having a karaoke session, or having a costume party, there are so many ways to have fun and create great memories with friends and family.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值