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
Source
题意:给你一个表达式,表达式中有p,q,r,s,t五个变量,以及K,A,N,C,E五个函数
求该表达式是否为永真式,即pqrst无论如何变化达标的的值始终是真的?
题解:既然有表达式嘛,那么肯定是要栈来做表达式求值,这题无非多了几个运算符和几次枚举而已,然而因为看错题意WA了好几次,唉……
代码如下:
#include<map>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fa puts("fuck");
using namespace std; char d[];
int vis[]= {,}; int K(int a,int b)
{
return a&b;
} int A(int a,int b)
{
return a|b;
} int N(int a)
{
return !a;
} int C(int a,int b)
{
return (!a)|b;
} int E(int a,int b)
{
return a==b;
} map<char,int> m; int dfs(int p,int q,int r,int s,int t)
{
int cnt;
stack<int> stack1;
for(int i=strlen(d)-; i>=; i--)
{
if(d[i]=='K'||d[i]=='A'||d[i]=='C'||d[i]=='E')
{
int tmp1=stack1.top();
stack1.pop();
int tmp2=stack1.top();
stack1.pop();
switch (d[i])
{
case 'K':
stack1.push(K(tmp1,tmp2));
break;
case 'A':
stack1.push(A(tmp1,tmp2));
break;
case 'C':
stack1.push(C(tmp1,tmp2));
break;
case 'E':
stack1.push(E(tmp1,tmp2));
}
}
else
{
if(d[i]=='N')
{
int tmp1=stack1.top();
stack1.pop();
stack1.push(!tmp1);
}
else
{
stack1.push(m[d[i]]);
}
}
}
int ans=stack1.top();
while(!stack1.empty())
{
stack1.pop();
}
return ans;
} int main()
{
while(scanf("%s",d),d[]!='')
{
memset(vis,,sizeof(vis));
for(int p=; p<=; p++)
{
m['p']=p;
for(int q=; q<=; q++)
{
m['q']=q;
for(int r=; r<=; r++)
{
m['r']=r;
for(int s=; s<=; s++)
{
m['s']=s;
for(int t=; t<=; t++)
{
m['t']=t;
vis[dfs(p,q,r,s,t)]=;
}
}
}
}
}
if(vis[])
{
puts("not");
}
else
{
puts("tautology");
}
} } //k(a,b)=a&b
//a(a,b)=a|b
//e(a)=!a
//c(a,b)=(!a)|b
//e(a,b)=!(a^b)
POJ3295 Tautology(栈+枚举)的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- 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
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...
- POJ3295 Tautology重言式
Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
随机推荐
- BMP格式介绍(一)
原理篇: 一.编码的意义. 让我们从一个简单的问题开始,-2&-255(中间的操作符表示and的意思)的结果是多少,这个很简单的问题,但是能够写出解答过程的人并不 多.这个看起来和图片格式没有 ...
- FPGA学习中的代码阅读
不管是学FPGA还是C语言,任何一种代码的学习都离不开大量的代码阅读,也就是多看,多学习别人的代码.初学者在学习的过程中更为重要的是模仿,模仿别人的代码算法怎么去处理的,模仿多了,代码看的多了,能力自 ...
- 使用Spring和Tomcat发布CXF REST WebService
与发布SOAP WS一样, 同样需要在web.xml文件中配置CXFServlet: <!--cxf的Servlet--> <servlet> <servlet-name ...
- log4net内部调试开启
最近用log4net写入日志到mysql数据库,死活写不进去,就想能不能看log4net的错误记录,在网上一找,还真有:开启log4net的内部调试,作个记录: <appSettings> ...
- bootstrap-datetimepicker如何只显示到日期
bootstrap-datetimepicker 一般都是设置到时分秒,有时候并不需要,怎么处理呢? minView: "month", //选择日期后,不会再跳转去选择时分秒 1 ...
- .Net 框架实现AOP(动态代理实现AOP,本文为翻译)
在上一节,我们将静态实现AOP,但是对于一个大型项目,要想为每个类,每个方法都去实现AOP ,进行日志记录和权限验证似乎是不可能的. 即使可能对于成百上千个类维护,也是很难维护.所以今天的主题就是如标 ...
- Fragment的陷阱:概述
现在主流的APP都会使用到Fragment,相信你也一定使用过,今天为大家介绍一下我曾经踏过的一个关于Fragment的坑. 以前做过的一个项目,Fragment嵌套高德地图,当再次进入Fragmen ...
- Oracle 11G的间隔(INTERVAL)分区
-- Create table create table MS_BIGTABLE_LOG ( record_date DATE, col_1 VARCHAR2(), col_2 VARCHAR2() ...
- AngularJS绑定数据
绑定数据总共有三种方式1.{{}}最常用2.ngbind3.ng-model 主要用在input标签
- Python 约束 , 自定义异常 , 加密 , 日志
约束 约束 , 约束其派生类: 保证派生类中必须编写send方法 , 不然执行可能就会报错 Python中 语法: class BaseMessage(object): def send(self ...