设为首页 加入收藏

TOP

poj-3295 Tautology
2015-07-24 06:28:43 来源: 作者: 【 】 浏览:35
Tags:poj-3295 Tautology

?

借助的是大神的解题报告的写法。

?

大致题意:

输入由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:输入格式保证是合法的。

代码操作:

1、把变量(p,q,r,s,t)替换成0,1。(替换有规律,见数组num,共32中可能);

2、用栈将字符串倒序当后缀式运算。

3、判断运算完成后栈底元素是否为1,如果不为0跳出输出not。

4、若32种可能都为1,则输出tautology;

?

#include
  
   
#include
   
     const int maxn=120; int sta[maxn]; //数组模拟堆栈 char str[maxn]; int p,q,r,s,t; void doit() { int top=0; int len=strlen(str); for(int i=len-1;i>=0;i--) { if(str[i]=='p') sta[top++]=p; else if(str[i]=='q') sta[top++]=q; else if(str[i]=='r') sta[top++]=r; else if(str[i]=='s') sta[top++]=s; else if(str[i]=='t') sta[top++]=t; else if(str[i]=='K') { int t1=sta[--top]; int t2=sta[--top]; sta[top++]=(t1&&t2); } else if(str[i]=='A') { int t1=sta[--top]; int t2=sta[--top]; sta[top++]=(t1||t2); } else if(str[i]=='N') { int t1=sta[--top]; sta[top++]=(!t1); } else if(str[i]=='C') { int t1=sta[--top]; int t2=sta[--top]; if(t1==1&&t2==0) sta[top++]=0; else sta[top++]=1; } else if(str[i]=='E') { int t1=sta[--top]; int t2=sta[--top]; if((t1==1&&t2==1)||(t1==0&&t2==0)) sta[top++]=1; else sta[top++]=0; } } } bool solve() { //5重循环,枚举2^5 32种可能 如果都满足 return 1 for(p=0;p<2;p++) for(q=0;q<2;q++) for(r=0;r<2;r++) for(s=0;s<2;s++) for(t=0;t<2;t++) { doit(); if(sta[0]==0)return 0; } return 1; } int main() { while(scanf(%s,&str)) { if(strcmp(str,0)==0)break; if(solve()) printf(tautology ); else printf(not ); } return 0; } 
   
  

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 1556 Color the Ball 线段树 .. 下一篇Swift 属性 函数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: