写完代码在自己机子上用样例数据直接通过了,提交结果RE,然后改。改了以后WA,一直不得其解。上无聊的课时又把代码拿出来看,原来少打了一条 break; 语句。。。
///2014.3.4 - 2014.3.6
///poj3295
/**
*大致题意:
*输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
*其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
*K、A、N、C、E为逻辑运算符,
*K --> and: x && y
*A --> or: x || y
*N --> not : ! x
*C --> implies : (!x)||y
*E --> equals : x==y
*问这个逻辑表达式是否为永真式。
*PS:输入格式保证是合法的
**
*思路:
*对于逻辑变量的每个取值,都依次枚举
*判断对于每一种逻辑变量的取值情况表达式是否为真
*/
#include <cstdio>
int pqrst; ///每一位表示一个逻辑变量
///表示表达式,ope代表操作符,length表示表达式长度
struct expression
{
char* ope;
int length;
};
///找表达式的左,右子表达式
void find_lr(expression exp,expression &l,expression &r)
{
if( *(exp.ope+1)<='t' && *(exp.ope+1)>='p' ){
l.ope = exp.ope+1 ;
l.length = 1;
r.ope = exp.ope+2 ;
r.length = exp.length-2;
}
else{
l.ope = exp.ope+1 ;
int i=1,n=1 ;
for( ; i<=n ; i++){
switch( *(exp.ope+i) ){
case 'K':
case 'A':
case 'C':
case 'E':
n += 2; break;
case 'N':
n += 1;
}
}
i--;
l.length = i;
if( *(exp.ope)=='N' ){
r = l;
}
else{
r.ope = l.ope + l.length;
r.length = exp.length - l.length - 1;
}
}
}
///对表达式求值
bool result(expression exp)
{
if( exp.length == 1 ){
switch( *(exp.ope) ){
case 'p':return pqrst & 0x1<<4 ;
case 'q':return pqrst & 0x1<<3 ;
case 'r':return pqrst & 0x1<<2 ;
case 's':return pqrst & 0x1<<1 ;
case 't':return pqrst & 0x1<<0 ;
}
}
else if( exp.length ==2 && *(exp.ope) == 'N' ){
switch( *(exp.ope+1) ){
case 'p':return !(pqrst & 0x1<<4) ;
case 'q':return !(pqrst & 0x1<<3) ;
case 'r':return !(pqrst & 0x1<<2) ;
case 's':return !(pqrst & 0x1<<1) ;
case 't':return !(pqrst & 0x1<<0) ;
}
}
else if( exp.length==3 && *(exp.ope)=='N' ){
switch( *(exp.ope+2) ){
case 'p':return (pqrst & 0x1<<4) ;
case 'q':return (pqrst & 0x1<<3) ;
case 'r':return (pqrst & 0x1<<2) ;
case 's':return (pqrst & 0x1<<1) ;
case 't':return (pqrst & 0x1<<0) ;
}
}
else{
expression l;
expression r;
find_lr(exp,l,r);
switch( *(exp.ope) ){
case 'K':
return result(l) && result(r);
case 'A':
return result(l) || result(r);
case 'C':
if( result(l) && !result(r) )
return false;
else
return true;
case 'E':
if( result(l)==result(r) )
return true;
else
return false;
case 'N':
return !result(l);
}
}
}
int main( )
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
char str[1100];
expression exp;
exp.ope = &str[0];
while( scanf("%s",exp.ope) && *(exp.ope) != '0' ){
for(exp.length = 0 ; *(exp.ope+exp.length)!='\0' ; exp.length++);
bool tautology = true;
for(pqrst=0 ; pqrst<32 && tautology ; pqrst++){
tautology = result(exp);
}
if( tautology )
printf("tautology\n");
else
printf("not\n");
}
return 0;
}