UVA 565 565 Pizza Anyone? (深搜 +位运算)
| Pizza Anyone? |
You are responsible for ordering a large pizza for you and your friends. Each of them has told you what he wants on a pizza and what he does not; of course they all understand that since there is only going to be one pizza, no one is likely to have all their requirements satisfied. Can you order a pizza that will satisfy at least one request from all your friends?
The pizza parlor you are calling offers the following pizza toppings; you can include or omit any of them in a pizza:
| Input Code | Topping |
| A | Anchovies |
| B | Black Olives |
| C | Canadian Bacon |
| D | Diced Garlic |
| E | Extra Cheese |
| F | Fresh Broccoli |
| G | Green Peppers |
| H | Ham |
| I | Italian Sausage |
| J | Jalapeno Peppers |
| K | Kielbasa |
| L | Lean Ground Beef |
| M | Mushrooms |
| N | Nonfat Feta Cheese |
| O | Onions |
| P | Pepperoni |
Your friends provide you with a line of text that describes their pizza preferences. For example, the line
+O-H+P;
reveals that someone will accept a pizza with onion, or without ham, or with pepperoni, and the line
-E-I-D+A+J;
indicates that someone else will accept a pizza that omits extra cheese, or Italian sausage, or diced garlic, or that includes anchovies or jalapenos.
Input
The input consists of a series of pizza constraints.
A pizza constraint is a list of 1 to 12 topping constraint lists each on a line by itself followed by a period on a line by itself.
A topping constraint list is a series of topping requests terminated by a single semicolon.
An topping request is a sign character (+/-) and then an uppercase letter from A to P.
Output
For each pizza constraint, provide a description of a pizza that satisfies it. A description is the string `` Toppings: " in columns 1 through 10 and then a series of letters, in alphabetical order, listing the toppings on the pizza. So, a pizza with onion, anchovies, fresh broccoli and Canadian bacon would be described by:
Toppings: ACFO
If no combination toppings can be found which satisfies at least one request of every person, your program should print the string
No pizza can satisfy these requests.
on a line by itself starting in column 1.
Sample Input
+A+B+C+D-E-F-G-H;
-A-B+C+D-E-F+G+H;
-A+B-C+D-E+F-G+H;
.
+A+B+C+D;
+E+F+F+H;
+A+B-G;
+O+J-F;
+H+I+C;
+P;
+O+M+L;
+M-L+P;
.
+A+B+C+D;
+E+F+F+H;
+A+B-G;
+P-O;
+O+J-F;
+H+I+C;
+P;
+O;
+O+M+L;
-O-P;
+M-L+P;
.
Sample Output
Toppings:
Toppings: CELP
No pizza can satisfy these requests.
题意:一个披萨有16种配料。现在有几个人要定一份披萨。每个人都有一定需求,+代表要哪种配料,-代表不要。
要找出一种披萨。来满足所有人至少一个需求。。如果找不到就输出No pizza can satisfy these requests.
思路:一共16种配料。选与不选每种配料2种选择,一共就是2^16种情况。暴力枚举。。结果超时了。。 因为每种披萨都要进行判断。判断的过程算进去就超时了。。没想出比较好的方法。。看到别人用位运算。。自己也试了下。结果就过了。。不过跑了500多MS。。不知道那些0.00几ms的大神怎么做的。。
位运算:把每个人需要与不需要,存成一个16位2进制数。然后在从0枚举到2 ^16 - 1代表每种披萨。如果有满足条件
他们的或运算会>0。。(关于位运算。稍微看下就能明白的)。。。然后就枚举直到有一种披萨符合条件。把该二进制数转换成相应披萨的字母输出。如果没有。就输出No pizza can satisfy these requests.
#include <stdio.h>
#include <string.h> struct Q
{
int yes;
int no;
} q[10005]; int num = 0;
int sta;
int out[20];
char str[105]; int main()
{
while (gets(str) != NULL)
{
if (str[0] == '.')
{
for (sta = 0; sta < (1 << 16); sta ++)
{
int i;
for (i = 0; i < num; i ++)
{
if ((q[i].yes & sta) || (q[i].no & (~sta)))
continue;
else
break;
}
if (i == num)
break;
}
int nnum = 0;
for (int i = 0; i < 16; i ++)
{
if (sta & (1 << i))
out[nnum ++] = i + 'A';
}
if (sta == (1 << 16))
printf("No pizza can satisfy these requests.\n");
else
{
printf("Toppings: ");
for (int i = 0; i < nnum; i ++)
printf("%c", out[i]);
printf("\n");
}
memset(q, 0, sizeof(q));
memset(out, 0, sizeof(out));
num = 0;
continue;
}
for (int i = 0; str[i] != ';'; i += 2)
{
if (str[i] == '+')
q[num].yes |= (1 << (str[i + 1] - 'A'));
if (str[i] == '-')
q[num].no |= (1 << (str[i + 1] - 'A'));
}
num ++;
}
return 0;
}
UVA 565 565 Pizza Anyone? (深搜 +位运算)的更多相关文章
- UVA 10160 Servicing Stations(深搜 + 剪枝)
Problem D: Servicing stations A company offers personal computers for sale in N towns (3 <= N < ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
- UVa 1103 Ancient Messages(二重深搜)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- 模拟赛T5 : domino ——深搜+剪枝+位运算优化
这道题涉及的知识点有点多... 所以还是比较有意思的. domino 描述 迈克生日那天收到一张 N*N 的表格(1 ≤ N ≤ 2000),每个格子里有一个非 负整数(整数范围 0~1000),迈克 ...
- hdu 5648 DZY Loves Math 组合数+深搜(子集法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...
- TOJ 4976: 新生数(深搜)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4976 时间限制(普通/Java): ...
- 天梯赛 L2-20 功夫传人 (深搜)
一门武功能否传承久远并被发扬光大,是要看缘分的.一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱-- 直到某一支的某一代突然出现一个天分特别高的弟子(或者是吃到了灵丹.挖到 ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- bzoj 1085骑士精神 迭代深搜
题目传送门 题目大意:给出一幅棋盘,问能否复原,中文题面,不做解释. 思路:第一次写迭代深搜的题目,这道题还是挺经典的.这道题的状态很明显的每多搜一层就是多八倍,非常的多,而且又是t组输入,所以必定有 ...
随机推荐
- NET Core 1.0
VS Code从零开始开发调试.NET Core 1.0 使用VS Code 从零开始开发调试.NET Core 1.0. .NET Core 是一个开源的.跨平台的 .NET 实现. VS Code ...
- 为什么希捷选择了8GB作为标准的闪存容量呢?答案就在下面(新的驱动器可以实现一定程度上的写入缓存)
与前两代Momentus XT产品不同,希捷称第三代混合硬盘所使用的技术更接近真正的SSHD(Solid State Hybrid Disk?),而不是HHD.也就是说更偏向于固态硬盘.首先它不会属于 ...
- Visual Studio 2015编译安装配置QT5.5.1(含QTWEBKIT)
尽管QT5.5.1和VisualStudio 2015都已经发布很久了,但是QT项目组视乎不会为QT5.5.1专门发布预编译的QT5.5.1 for windows(2015)版本的,也不会专门发布V ...
- poj3308 Paratroopers --- 最小点权覆盖->最小割
题目是一个非常明显的二分图带权匹配模型, 加入源点到nx建边,ny到汇点建边,(nx.ny)=inf建边.求最小割既得最小点权覆盖. 在本题中因为求的是乘积,所以先所有取log转换为加法,最后再乘方回 ...
- BZOJ 2429: [HAOI2006]聪明的猴子( MST )
水题, 求MST即可. -------------------------------------------------------------------------------- #includ ...
- Python 第五篇(上):算法、自定义模块、系统标准模块(time 、datetime 、random 、OS 、sys 、hashlib 、json和pickle)
一:算法回顾: 冒泡算法,也叫冒泡排序,其特点如下: 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应 ...
- 探究绑定事件的this指向以及event传参的小问题
this指向 直接上代码,凑合着看 <input type="button" onclick="page()" value="点我"& ...
- 私有析构函数 Android 代码分析
有人说声明 Private Destructor, 这对象只能在 stack 上创建,不能在Heap上创建, 其实错了, 这样的程序编译都过不了. 那为何会有 Private Destructor, ...
- Eclipse用法和技巧二十二:快速调整字体大小
团队代码review的时候,一般都会一堆人围着显示器,或者投影仪.这个时候调整代码字体大小就显得很重要.下面直接说操作方式. 步骤一:Windows -> Preference 步 ...
- [REST Jersey] @QueryParam Demo
This demo sourced from the jersey tutor. https://jersey.java.net/documentation/latest/jaxrs-resource ...