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 ...
随机推荐
- 【转】关于一个Jmeter interface testing的实例
目标:测试某个保险系统的费率接口 准备:a 请求方式:Http b 接口地址://10.1.1.223:9090/rulesEngine/executeRateRule.do Jmeter 设置: a ...
- logging模块讲解
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- phpmailer实现邮件发送
phpmailer实现邮件发送 1.代码 <?php require("class.phpmailer.php"); //这个是一个smtp的php文档,网上可以下载得到 $ ...
- 《PHP对象、模式与实践》之高级特性
高级特性包括:1.静态方法和属性(通过类而不是对象来访问数据和功能)2.抽象类和接口(设计,实现分离)3.错误处理(异常)4.Final类和方法(限制继承)5.拦截器(自动委托)6.析构方法(对象销毁 ...
- zabbix 在linux上安装以及一些配置
本文章将演示zabbix 3.2版本的安装,供有需要的伙伴们参考: 网络也有很多关于zabbix的安装文档,甚至每一步的配置都有详细的截图,我这里就不演示截图了,多配置几次自然就熟练了.多折腾. 楼主 ...
- python学习(十八) 程序打包
18.1 Distutils基础 18.2 打包 18.2.1 建立存档文件 18.2.2 创建Windows安装程序或RPM包 18.3 编译扩展 18.4 使用py2exe创建可执行程序
- Javascript 推荐一个图形化展示库
觉得这个库不错: http://almende.github.io/chap-links-library/index.html
- Docker构建ssh镜像
FROM ubuntu MAINTAINER ggzone xxx@live.com ENV REFRESHED_AT 2015-10-21 RUN apt-get -qqy update & ...
- java-虚拟机-索引
底层 JVM之堆内存(年经代,老年代) Java内存泄露的理解与解决 内存溢出和内存泄漏的区别.产生原因以及解决方案 JVM内容梳理 Jvm的体系结构
- Hadoop的HA机制
前言:正式引入HA机制是从hadoop2.0开始,之前的版本中没有HA机制 1. HA的运作机制 (1)hadoop-HA集群运作机制介绍 所谓HA,即高可用(7*24小时不中断服务) 实现高可用最关 ...