题意:要给朋友们订一份披萨,每个人喜好不同,列出了朋友们对披萨的要求,+A代表喜欢A口味,-A代表讨厌A口味,只需要满足每个朋友的一种需求就可以,然后输出披萨放的全部口味,否则输出NO..
题解:因为16种口味都有两种情况放或者不放,所以共有2^16 - 1种情况,把所有的情况用二进制都枚举出来,然后和输入的每个人比对就可以得出结果。
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int N = 100;
string s[N];
int n;
struct ST {
int ye, no;
}sta[N];
void init() {
n = 0;
memset(sta, 0, sizeof(sta));
}
int main() {
init();
while (cin >> s[n]) {
if (s[n++][0] == '.') {
n--;
int i, j;
for (i = 0; i < n; i++) {
int len = s[i].size();
for (j = 0; j < len - 1; j += 2)
if (s[i][j] == '+')
sta[i].ye |= (1 << (s[i][j + 1] - 'A'));
else
sta[i].no |= (1 << (s[i][j + 1] - 'A'));
}
for (i = 0; i < (1 << 16); i++) {
for (j = 0; j < n; j++) {
if ((sta[j].ye & i) || (sta[j].no & (~i)))
continue;
else
break;
}
if (j == n)
break;
}
if (i == (1 << 16))
printf("No pizza can satisfy these requests.\n");
else {
printf("Toppings: ");
for (int j = 0; j < 16; j++)
if (i & (1 << j))
printf("%c", j + 'A');
printf("\n");
}
init();
}
}
return 0;
}