poj3295 Tautology
写完代码在自己机子上用样例数据直接通过了,提交结果RE,然后改。改了以后WA,一直不得其解。上无聊的课时又把代码拿出来看,原来少打了一条 break; 语句。。。
1 ///2014.3.4 - 2014.3.6 2 ///poj3295 3 4 /** 5 *大致题意: 6 *输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式, 7 *其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量; 8 *K、A、N、C、E为逻辑运算符, 9 *K --> and: x && y 10 *A --> or: x || y 11 *N --> not : ! x 12 *C --> implies : (!x)||y 13 *E --> equals : x==y 14 *问这个逻辑表达式是否为永真式。 15 *PS:输入格式保证是合法的 16 ** 17 *思路: 18 *对于逻辑变量的每个取值,都依次枚举 19 *判断对于每一种逻辑变量的取值情况表达式是否为真 20 */ 21 22 #include <cstdio> 23 24 int pqrst; ///每一位表示一个逻辑变量 25 26 ///表示表达式,ope代表操作符,length表示表达式长度 27 struct expression 28 { 29 char* ope; 30 int length; 31 }; 32 33 ///找表达式的左,右子表达式 34 void find_lr(expression exp,expression &l,expression &r) 35 { 36 if( *(exp.ope+1)<='t' && *(exp.ope+1)>='p' ){ 37 l.ope = exp.ope+1 ; 38 l.length = 1; 39 r.ope = exp.ope+2 ; 40 r.length = exp.length-2; 41 } 42 else{ 43 l.ope = exp.ope+1 ; 44 int i=1,n=1 ; 45 for( ; i<=n ; i++){ 46 switch( *(exp.ope+i) ){ 47 case 'K': 48 case 'A': 49 case 'C': 50 case 'E': 51 n += 2; break; 52 case 'N': 53 n += 1; 54 } 55 } 56 i--; 57 l.length = i; 58 if( *(exp.ope)=='N' ){ 59 r = l; 60 } 61 else{ 62 r.ope = l.ope + l.length; 63 r.length = exp.length - l.length - 1; 64 } 65 } 66 } 67 68 ///对表达式求值 69 bool result(expression exp) 70 { 71 if( exp.length == 1 ){ 72 switch( *(exp.ope) ){ 73 case 'p':return pqrst & 0x1<<4 ; 74 case 'q':return pqrst & 0x1<<3 ; 75 case 'r':return pqrst & 0x1<<2 ; 76 case 's':return pqrst & 0x1<<1 ; 77 case 't':return pqrst & 0x1<<0 ; 78 } 79 } 80 else if( exp.length ==2 && *(exp.ope) == 'N' ){ 81 switch( *(exp.ope+1) ){ 82 case 'p':return !(pqrst & 0x1<<4) ; 83 case 'q':return !(pqrst & 0x1<<3) ; 84 case 'r':return !(pqrst & 0x1<<2) ; 85 case 's':return !(pqrst & 0x1<<1) ; 86 case 't':return !(pqrst & 0x1<<0) ; 87 } 88 } 89 else if( exp.length==3 && *(exp.ope)=='N' ){ 90 switch( *(exp.ope+2) ){ 91 case 'p':return (pqrst & 0x1<<4) ; 92 case 'q':return (pqrst & 0x1<<3) ; 93 case 'r':return (pqrst & 0x1<<2) ; 94 case 's':return (pqrst & 0x1<<1) ; 95 case 't':return (pqrst & 0x1<<0) ; 96 } 97 } 98 99 else{ 100 expression l; 101 expression r; 102 find_lr(exp,l,r); 103 switch( *(exp.ope) ){ 104 case 'K': 105 return result(l) && result(r); 106 case 'A': 107 return result(l) || result(r); 108 case 'C': 109 if( result(l) && !result(r) ) 110 return false; 111 else 112 return true; 113 case 'E': 114 if( result(l)==result(r) ) 115 return true; 116 else 117 return false; 118 case 'N': 119 return !result(l); 120 } 121 } 122 } 123 124 int main( ) 125 { 126 // freopen("in","r",stdin); 127 // freopen("out","w",stdout); 128 129 char str[1100]; 130 expression exp; 131 exp.ope = &str[0]; 132 while( scanf("%s",exp.ope) && *(exp.ope) != '0' ){ 133 for(exp.length = 0 ; *(exp.ope+exp.length)!='\0' ; exp.length++); 134 135 bool tautology = true; 136 for(pqrst=0 ; pqrst<32 && tautology ; pqrst++){ 137 tautology = result(exp); 138 } 139 140 if( tautology ) 141 printf("tautology\n"); 142 else 143 printf("not\n"); 144 } 145 return 0; 146 }