神一样的短路。对于单目运算符,如果对于true和false都输出相同的东西就直接短路了,双目运算符可以左边短路,右边短路,还可以两边一起短路……给跪了……
/* Author : Speedcell Update : 2013-05-30 Version : soppYcell 2.2(a) */ #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <map> #include <set> #include <list> #include <stack> #include <queue> #include <deque> #include <vector> #include <string> #include <bitset> #include <memory> #include <complex> #include <numeric> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time.h> #include <ctype.h> #include <locale.h> using namespace std; #pragma pack(4) #ifndef __CONSTANT__ #define __CONSTANT__ typedef long long LONG; const double pi = acos(-1.0); const int inf = 0x7f7f7f7f; const LONG INF = 0x7f7f7f7f7f7f7f7fll; const int go[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; #endif // __CONSTANT__ #ifndef __IO__ #define __IO__ inline bool RD(int & a) {return scanf("%d",&a)!=EOF;} inline bool RD(char & a) {return scanf("%c",&a)!=EOF;} inline bool RD(char * a) {return scanf("%s", a)!=EOF;} inline bool RD(double & a) {return scanf("%lf",&a)!=EOF;} inline bool RD(LONG & a) {return scanf("%I64d",&a)!=EOF;} template<class T1> inline bool IN(T1 & a) {return RD(a);} template<class T1,class T2> inline bool IN(T1 & a,T2 & b) {return RD(a)&&RD(b);} template<class T1,class T2,class T3> inline bool IN(T1 & a,T2 & b,T3 & c) {return RD(a)&&RD(b)&&RD(c);} template<class T1,class T2,class T3,class T4> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d) {return RD(a)&&RD(b)&&RD(c)&&RD(d);} template<class T1,class T2,class T3,class T4,class T5> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e);} template<class T1,class T2,class T3,class T4,class T5,class T6> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f);} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline bool IN(T1 & a,T2 & b,T3 & c,T4 & d,T5 & e,T6 & f,T7 & g) {return RD(a)&&RD(b)&&RD(c)&&RD(d)&&RD(e)&&RD(f)&&RD(g);} inline void PT(int a) {printf("%d",a);} inline void PT(char a) {printf("%c",a);} inline void PT(char * a) {printf("%s",a);} inline void PT(double a) {printf("%f",a);} inline void PT(LONG a) {printf("%I64d",a);} inline void PT(const char a[]) {printf("%s",a);} template<class T1> inline void OT(T1 a) {PT(a);} template<class T1,class T2> inline void OT(T1 a,T2 b) {PT(a),PT(' '),PT(b);} template<class T1,class T2,class T3> inline void OT(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PT(c);} template<class T1,class T2,class T3,class T4> inline void OT(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d);} template<class T1,class T2,class T3,class T4,class T5> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e);} template<class T1,class T2,class T3,class T4,class T5,class T6> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f);} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void OT(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PT(g);} template<class T1> inline void OL(T1 a) {PT(a),PT('\n');} template<class T1,class T2> inline void OL(T1 a,T2 b) {PT(a),PT(' '),PT(b),PT('\n');} template<class T1,class T2,class T3> inline void OL(T1 a,T2 b,T3 c) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT('\n');} template<class T1,class T2,class T3,class T4> inline void OL(T1 a,T2 b,T3 c,T4 d) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT('\n');} template<class T1,class T2,class T3,class T4,class T5> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT('\n');} template<class T1,class T2,class T3,class T4,class T5,class T6> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT('\n');} template<class T1,class T2,class T3,class T4,class T5,class T6,class T7> inline void OL(T1 a,T2 b,T3 c,T4 d,T5 e,T6 f,T7 g) {PT(a),PT(' '),PT(b),PT(' '),PT(c),PT(' '),PT(d),PT(' '),PT(e),PT(' '),PT(f),PT(' '),PT(g),PT('\n');} #endif // __IO__ #ifndef __MACRO__ #define __MACRO__ #define ML(times) int tcase; IN(tcase); FOR(times,1,tcase) #define FOR(i,a,b) for(int i=int(a),_##i=int(b);i<=_##i;i++) #define DWN(i,b,a) for(int i=int(b),_##i=int(a);_##i<=i;i--) #define ECH(i,u,pre,next) for(int i=int(pre[u]);i!=-1;i=int(next[i])) #define MEM(a,v) memset(a,v,sizeof(a)) #define CLR(a,v) FOR(_i##a,0,sizeof(a)/sizeof(a[0])-1) a[_i##a]=v #define LOOP(a,n) \ FOR(_i##a,0,(n)-1) \ OT(a[_i##a]),OT(_i##a!=__i##a?' ':'\n') #define LOOP2(a,n,m) \ FOR(_i##a,0,(n)-1) FOR(_j##a,0,(m)-1) \ OT(a[_i##a][_j##a]),OT(_j##a!=__j##a?' ':'\n') #define LOOPG(G,n,pre,next) \ FOR(_i##a,0,(n)-1) ECH(_j##a,_i##a,pre,next) \ OL(_i##a,G[_j##a].v,G[_j##a].w) #endif // __MACRO__ #ifndef __BIT__ #define __BIT__ template<class T> inline T lb(T i) {return i&-i;} template<class T> inline T lc(T i) {return i<<1;} template<class T> inline T rc(T i) {return i<<1|1;} template<class T> inline T at(T a,int i) {return a& (T(1)<<i);} template<class T> inline T nt(T a,int i) {return a^ (T(1)<<i);} template<class T> inline T s1(T a,int i) {return a| (T(1)<<i);} template<class T> inline T s0(T a,int i) {return a&~(T(1)<<i);} #endif // __BIT__ #ifndef __DOUBLE__ #define __DOUBLE__ const double eps = 1e-8; inline int cmp(double a,double b=0) {return fabs(a-b)<eps?0:((a-b)>eps?+1:-1);} inline double fmax(double a,double b) {return cmp(a,b)>0?a:b;} inline double fmin(double a,double b) {return cmp(a,b)<0?a:b;} #endif // __DOUBLE__ int n,m,times=1; vector<string> one; map<string,int> belong; string name,val1,val2,expr; map<string,map<string,string> > unary; map<string,map<string,map<string,string> > > binary; bool check(int l,int r) { if(one[l]!="("||one[r]!=")") return false; else { int cnt=0; FOR(i,l,r-1) { if(one[i]=="(") cnt++; else if(one[i]==")") cnt--; if(!cnt) return false; } return true; } } string solve(int l,int r) { if(l==r) { if(one[l]!="true"&&one[l]!="false") return "unknown"; else return one[l]; } else if(check(l,r)) return solve(l+1,r-1); else { int cnt=0; FOR(i,l,r) { if(one[i]=="(") cnt++; else if(one[i]==")") cnt--; if(cnt) continue; if(belong[one[i]]==2) { string left=solve(l,i-1); string right=solve(i+1,r); if(binary[one[i]]["false"]["false"]==binary[one[i]]["false"]["true"]&& binary[one[i]]["false"]["false"]==binary[one[i]]["true"]["false"]&& binary[one[i]]["false"]["false"]==binary[one[i]]["true"]["true"]) return binary[one[i]]["false"]["false"]; else if(left!="unknown"&&binary[one[i]][left]["true"]==binary[one[i]][left]["false"]) return binary[one[i]][left]["true"]; else if(right!="unknown"&&binary[one[i]]["true"][right]==binary[one[i]]["false"][right]) return binary[one[i]]["true"][right]; else if(left!="unknown"&&right!="unknown") return binary[one[i]][left][right]; else return "unknown"; } } string right=solve(l+1,r); if(unary[one[l]]["true"]==unary[one[l]]["false"]) return unary[one[l]]["true"]; else if(right!="true"&&right!="false") return "unknown"; else return unary[one[l]][right]; } } int main() { #ifndef ONLINE_JUDGE freopen("Evaluating Logic Expressions.txt","r",stdin); #else #endif while(cin>>n>>m,n!=-1||m!=-1) { one.clear(); unary.clear(); belong.clear(); binary.clear(); FOR(i,0,n-1) { cin>>name; belong[name]=1; cin>>val1>>val2; unary[name]["false"]=val1; unary[name]["true"]=val2; } FOR(i,0,m-1) { cin>>name; belong[name]=2; cin>>val1>>val2; binary[name]["false"]["false"]=val1; binary[name]["false"]["true"]=val2; cin>>val1>>val2; binary[name]["true"]["false"]=val1; binary[name]["true"]["true"]=val2; } getchar(); getline(cin,expr); int from=0; FOR(i,0,expr.length()) { if(expr[i]=='(') { if(from!=i) one.push_back(string(expr,from,i-from)); one.push_back("("); from=i+1; } else if(expr[i]==')') { if(from!=i) one.push_back(string(expr,from,i-from)); one.push_back(")"); from=i+1; } else if(expr[i]==' '||expr[i]=='\0') { if(from!=i) one.push_back(string(expr,from,i-from)); from=i+1; } } while(cin>>name,name!="*") { cin>>val1; FOR(i,0,one.size()-1) { if(one[i]==name) one[i]=val1; } } cout<<"Case "<<times++<<": "<<solve(0,one.size()-1)<<endl; } return 0; }