Tautology
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 |
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
ApNp
ApNq
0
Sample Output
tautology
not 就是离散数学的一个模拟,判断所给的式子是不是永真公式
用栈逆序推还不算难
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
stack<int>sta;
int p,q,r,s,t,a,b;
char str[];
int f()
{
int len=strlen(str);
for(int i=len-; i>-; i--)
{
if(str[i]=='p')sta.push(p);//是字符直接入栈
else if(str[i]=='q')sta.push(q);
else if(str[i]=='r')sta.push(r);
else if(str[i]=='s')sta.push(s);
else if(str[i]=='t')sta.push(t);
else if(str[i]=='K')//是运算就进行运算
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
sta.push(a&&b);
}
else if(str[i]=='A')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
sta.push(a||b);
}
else if(str[i]=='C')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
if(a&&!b)sta.push();
else sta.push();
}
else if(str[i]=='E')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
if(a==b)sta.push();
else sta.push();
}
else if(str[i]=='N')
{
a=sta.top();
sta.pop();
sta.push(!a);
}
}
return sta.top();
}
int sf()//枚举各种情况
{
for(p=; p<; p++)
for(q=; q<; q++)
for(r=; r<; r++)
for(s=; s<; s++)
for(t=; t<; t++)
if(!f())
return ;
return ;
}
int main()
{
while(gets(str))
{
if(!strcmp(str,""))
break;
else
{
if(sf())
printf("tautology\n");
else
printf("not\n");
}
}
return ;
}
Tautology的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- Tautology(structure)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10061 Accepted: 3826 Descri ...
- Tautology 分类: POJ 2015-06-28 18:40 10人阅读 评论(0) 收藏
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10428 Accepted: 3959 Descri ...
- poj 3295 Tautology
点击打开链接 Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8127 Accepted: 3115 ...
- POJ3295——Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- poj 3295 Tautology (构造)
题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
随机推荐
- const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏
const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. 事实上这个概念谁都有只是三种声明方式非常相似很容易记混. Bjarne在他的 ...
- jQuery 插件开发 笔记
JQuery 插件开发: 类级别开发,开发新的全局函数 对象级别开发,给Jquery对象开发新方法 一.类级别开发 -定义全局方法 jQuery.foo = function() { alert('T ...
- Android(java)学习笔记197:常用的对话框
一.常见对话框属性: 1. AlertDialog.Builder属性 • setTitle: 为对话框设置标题 :• setIcon : 为对话框设置图标:• setMessage: 为对话框设置 ...
- UICollectionView reloadData后cell被隐藏
在使用UICollectionView的页面执行: [self.collectionView reloadData]; 执行后,页面变为空白页,调试发现,执行reloadData 后UICollect ...
- RSA签名验签
import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...
- 我的博客css得到别人的认可
你好 在吗?? 本消息来自QQ临时会话,如果您收到骚扰信息,可点击以下网址中的"停用服务"关闭临时会话: http://shang.qq.com/widget/set.php 20 ...
- 怎么让自己的java系统使用支付接口
昨天花了好久的时间学习了支付接口的教,我看了前7集,就够用了,大家上网搜索一下传智播客在线支付还不错. 1.一开始有一个form表单 2.这个表单是他帮你写好的,有很多银行,银行的name都是特定的 ...
- mysql - 编码
show variables like 'character%'; 通过以上命令可以查询编码情况, 不过,在安装的时候,建议选择‘gbk’这类中文编码, 如果选择的是utf8,则在处理的过程中需要进行 ...
- 那些年,我们一起学WCF--(6)PerCall实例行为
当客户端调用服务器端服务后,服务器端就会为客户端生成一个实例,关于服务实例的分配问题,在WCF中有专门的属性进行设置,可以让所有客户端共享一个实例, 也可以让一个客户端可以拥有多个实例,也可以让一个实 ...
- oracle行列转换总结-转载自ITPUB
原贴地址:http://www.itpub.net/thread-1017026-1-1.html 谢谢原贴大人 最近论坛很多人提的问题都与行列转换有关系,所以我对行列转换的相关知识做了一个总结, 希 ...