POJ3295——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.
| 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 题目大意:逻辑表达式求知,K, A, N, C, E为逻辑运算符,p, q, r, s, and t 为真假值。
Kxy -> x&&y
Axy -> x||y
Nx -> !x
Cxy -> x||(!y)
Exy -> x==y
判断是否表达式恒为真。
解题思路: 数值变量总共就5个,枚举这五个变量的值,有32种情况。
处理字符串的时候,类似于逆波兰表达式的求值过程。
从(S.length()-1)->0遍历,遇到小写字母将其对应的布尔值存入栈。
遇到大写字母(除N外) 去除栈顶2个元素进行处理,后存入栈。
遇到N,去除栈顶一个元素,取反后存入栈。
遍历完返回S.top()。
Code:
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int q,p,s,r,t;
bool Is_Tau(string S)
{
int len=S.length()-,i,t1,t2;
stack<char> ST;
for (i=len;i>=;i--)
{
if (S[i]=='q') ST.push(q);
if (S[i]=='p') ST.push(p);
if (S[i]=='r') ST.push(r);
if (S[i]=='s') ST.push(s);
if (S[i]=='t') ST.push(t);
if (S[i]=='K')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1&&t2);
}
if (S[i]=='A')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1||t2);
}
if (S[i]=='C')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1||(!t2));
}
if (S[i]=='E')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1==t2);
}
if (S[i]=='N')
{
t1=ST.top();
ST.pop();
ST.push(!t1);
}
}
return ST.top();
}
int main()
{
string WFF;
while (cin>>WFF)
{
int OK=;
if (WFF=="") break;
for (q=; q<=; q++)
for (p=; p<=; p++)
for (r=; r<=; r++)
for (s=; s<=; s++)
for (t=; t<=; t++)
if (!Is_Tau(WFF))
{
OK=;
break;
}
if (OK) printf("tautology\n");
else printf("not\n");
}
return ;
}
POJ3295——Tautology的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...
- poj3295 Tautology —— 构造法
题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- POJ3295 Tautology 解题报告
直接上分析: 首先 弄清各种大写字母的操作的实质 K 明显 是 and & A 是 or | N 是 not ! C 由表格注意到 当 w<=x 时 值为1 E 当 ...
- POJ3295 Tautology重言式
Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
随机推荐
- 添加点标注IMarkerElement
private void AddPointElement(IPoint pPoint) { if (pPoint != null) { IElement pElement = null; IRgbCo ...
- AE实现投影定义和投影转换
添加引用ESRI.ArcGIS.DataManagementTools 1.获取要定义和要转换的投影 IWorkspaceFactory wsf = new ShapefileWorkspaceFac ...
- 【转】VS2012发布网站详细步骤
1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布: 2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 输入你自 ...
- php curl基本操作
如何使用cURL的基本方法?首先,修改php.ini文件的设置,找到php_curl.dll,取消下在的注释extension=php_curl.dll,因为php默认是不开启cURL的. cURL是 ...
- call callb callp区别
对于刚刚接触ILE模式开发的初级菜鸟而言,想要搞清楚这三者的区别还是有点难度的.网上虽然一些帖子对这三者进行了比较,但是这些帖子或是语焉不详,或是高度概括.对于老鸟来说或许已经足矣,但是对于初级菜鸟而 ...
- zhuan:ubuntu下安装Apache2+php+Mysql
from: http://www.cnblogs.com/lynch_world/archive/2012/01/06/2314717.html ubuntu下安装Apache+PHP+Mysql 转 ...
- shapefile文件
基本信息编辑 ESRI公司的Shapefile文件是描述空间数据的几何和属性特征的非拓扑实体矢量数据结构的一种格式. 内容编辑 一个Shapefile文件最少包括三个文件: 主文件(*.shp).-- ...
- js截取所需字符串长度
//title :字符串 :interceptLength:所需的长度 function TitleThumbnail(title, interceptLength, thumbnailCharac ...
- vijos 1085 Sunnypig闯三角关
{这个题5个正确,五个超时,不要盲目相信我的代码,谁有更好的算法或者优化请留言,(*^__^*) 嘻嘻……} 背景 贪玩的sunnypig请Charles为他打造一个奇幻世界,Charles欣然答应了 ...
- linux源码分析2
linux源码分析 这里使用的linux版本是4.8,x86体系. 这篇是 http://home.ustc.edu.cn/~boj/courses/linux_kernel/1_boot.html ...