POJ 3295:Tautology
2015-07-22 09:16
330 查看
Tautology
Description
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:
p, q, r, s, and t are WFFs
if w is a WFF, Nw is a WFF
if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
The meaning of a WFF is defined as follows:
p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
A tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the
value 0 for p=0, q=1.
You must determine whether or not a WFF is a tautology.
Input
Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
Sample Output
这个题记得是离散数学里面的内容,题意是判断给定的字符串是不是永远为1,就是不管p、q、r、s、t取什么值,其结果都是1。
1AC。反正自从遇到了上一次类似的题目之后,做这种题自己的感受就是两点:
1.构造一个栈
2.从后往前面撸。
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10482 | Accepted: 3982 |
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:
p, q, r, s, and t are WFFs
if w is a WFF, Nw is a WFF
if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
The meaning of a WFF is defined as follows:
p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E |
w x | Kwx | Awx | Nw | Cwx | Ewx |
1 1 | 1 | 1 | 0 | 1 | 1 |
1 0 | 0 | 1 | 0 | 0 | 0 |
0 1 | 0 | 1 | 1 | 1 | 0 |
0 0 | 0 | 0 | 1 | 1 | 1 |
value 0 for p=0, q=1.
You must determine whether or not a WFF is a tautology.
Input
Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
ApNp ApNq 0
Sample Output
tautology not
这个题记得是离散数学里面的内容,题意是判断给定的字符串是不是永远为1,就是不管p、q、r、s、t取什么值,其结果都是1。
1AC。反正自从遇到了上一次类似的题目之后,做这种题自己的感受就是两点:
1.构造一个栈
2.从后往前面撸。
代码:
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <stack> #include <cmath> using namespace std; stack <int> o_sta; int p,q,r,s,t; string test; int len,i; void push1(char a) { switch (a) { case 'p': o_sta.push(p); break; case 'q': o_sta.push(q); break; case 'r': o_sta.push(r); break; case 's': o_sta.push(s); break; case 't': o_sta.push(t); break; default: break; } } void cal(char a) { int temp1,temp2; switch (a) { case 'N': temp1=o_sta.top(); o_sta.pop(); temp1=!temp1; o_sta.push(temp1); break; case 'K': temp1=o_sta.top(); o_sta.pop(); temp2=o_sta.top(); o_sta.pop(); temp1=temp1&temp2; o_sta.push(temp1); break; case 'A': temp1=o_sta.top(); o_sta.pop(); temp2=o_sta.top(); o_sta.pop(); temp1=temp1|temp2; o_sta.push(temp1); break; case 'C': temp1=o_sta.top(); o_sta.pop(); temp2=o_sta.top(); o_sta.pop(); temp1=temp1-temp2; if(temp1==1) o_sta.push(0); else o_sta.push(1); break; case 'E': temp1=o_sta.top(); o_sta.pop(); temp2=o_sta.top(); o_sta.pop(); temp1=temp1-temp2; if(temp1==0) o_sta.push(1); else o_sta.push(0); break; default: break; } } bool solve() { for(p=0;p<=1;p++) { for(q=0;q<=1;q++) { for(r=0;r<=1;r++) { for(s=0;s<=1;s++) { for(t=0;t<=1;t++) { for(i=len-1;i>=0;i--) { if(test[i]=='p'||test[i]=='q'||test[i]=='r'||test[i]=='s'||test[i]=='t') push1(test[i]); else cal(test[i]); } if(o_sta.top()==0) return false; } } } } } return true; } int main() { while(cin>>test) { if(test=="0") break; len=test.length(); if(solve()) { cout<<"tautology"<<endl; } else { cout<<"not"<<endl; } } //system("pause"); return 0; }
相关文章推荐
- LRU算法的最小堆实现
- Nginx 配置php-fpm(或者Fast_cgi等等)——File Not find!
- 会话EJB系列(四)SessionBean事务管理
- 图表中如何实现动态变更分类轴与系列值
- .NET系统开发过程中积累的扩展方法
- 再探LIS
- Intellj IDEA14.0.2启动异常之3分钟修复
- Java正则表达式 Matcher类成员函数 matches find lookingAt group start end 用法
- 黑马66期android学习笔记14-电话拨号器定义布局&获取组件对象
- 第一个OC程序
- 【HTML】html5新属性-datalist
- java学习-------异常
- Android基础入门教程——3.3 Handler消息传递机制浅析
- 对重装系统彻底说再见――电脑C盘备份
- Android surfaceview详解
- 由餐厅管理引发的思考
- 你了解软件安全性易用性吗?
- 目录、目录项、超级块、索引节点、文件
- 常量与只读变量
- Android基础入门教程——3.2 基于回调的事件处理机制