一道水题,觉得用了点小技巧就放上来了。
首先是读入,前些天看了本省rank1的NOIP2017时间复杂度的代码,%%%全省最短(100分里面),读入用的是自定义的快读。说实话,以后字符串读入都用这种方式就好了,挺方便的。
还有就是用二进制表示状态,前几天考试也是靠这个骗了80分,最近还学了“位图”这种神奇的东西,感觉整个人都二进制了。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include<iostream> #include<cstdio> using namespace std; const int inf=0x3f3f3f3f; bool jud[1<<17]; struct person{ int a,b; }p[25]; int t; int f; inline int read(){ char ch='+';f=1; while(!isalpha(ch)){if(ch=='-')f=-1;if(ch==';')return inf;if(ch=='.')return -inf;ch=getchar();} return (ch-'A'); } bool judge(int sta){ for(int i=1;i<t;i++){ if((sta&p[i].a)||(~sta)&p[i].b)continue; return 0; } return 1; } void out(int sta){ printf("Toppings: "); int k=0; while(sta){ if(sta&1)printf("%c",k+'A'); k++; sta>>=1; } } int main(){ t=1; while(1){ int tmp=read(); if(tmp==inf){t++;continue;} if(tmp==-inf)break; if(f==-1)p[t].b|=(1<<(tmp)); else p[t].a|=(1<<tmp); } for(int i=0;i<=(1<<16);i++){ if(judge(i)){ out(i); return 0; } } printf("No pizza can satisfy these requests."); return 0; } |
评论