/*直接按客人喜好进行回溯就可以了,交POJ会WA。POJ貌似不是多解*/
#include<stdio.h>
#include<string.h>
#include<time.h>
char like[20][100],pizza[50];
int vis[20];
int n,flag;
int DFS(int cur)
{
int i,j,k;
if(flag)return 0; //This sentence is very important.2.316s vs 0.084s
if(cur==n-1)
{
k=0;
for(j=0;j<16;j++)
{
if(vis[j]==1)pizza[k++]=j+'A';
}
pizza[k]='\0';
flag=1;
return 1;
}
for(i=0;i<strlen(like[cur]);i+=2)
{
if(like[cur][i]=='+')
{
if(vis[like[cur][i+1]-'A']==1)DFS(cur+1);
else if(!vis[like[cur][i+1]-'A'])
{
vis[like[cur][i+1]-'A']=1;
if(DFS(cur+1))return 1;
vis[like[cur][i+1]-'A']=0;
}
}
else if(like[cur][i]=='-')
{
if(vis[like[cur][i+1]-'A']==-1)DFS(cur+1);
else if(!vis[like[cur][i+1]-'A'])
{
vis[like[cur][i+1]-'A']=-1;
if(DFS(cur+1))return 1;
vis[like[cur][i+1]-'A']=0;
}
}
}
return 0;
}
int main()
{
int i=0;
while(scanf("%s",like[i++])!=EOF)
{
if(like[i-1][0]=='.')
{
n=i;
flag=0;
memset(vis,0,sizeof(vis));
memset(pizza,0,sizeof(pizza));
DFS(0);
if(flag)
{
printf("Toppings: ");
puts(pizza);
}
else printf("No pizza can satisfy these requests.\n");
i=0;
}
}
return 0;
}
二进制法暴力枚举可以过POJ,但是过不了UVA。