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 ...
随机推荐
- mysql 执行批量的sql
<?xml version="1.0" encoding="utf-8" ?> <dbconfig> <!-- 数据库驱动 --& ...
- FIREDAC的心得
FIREDAC与UNIDAC有些不同 但大体上是相同的 以下是一些随手笔记: FieldCount是当前FDQuery2所在行里面有多少列 一般用FieldList[X]来代表第几列 str:=FDQ ...
- 解决使用Qt creator时出现Cannot overwrite file ..Permission denied
前两天在linux下使用Qt creator, 切换到了管理员使用了Qt creator后,再切换为普通用户,发现出现了 Cannot overwrite file ..Permission deni ...
- 1131 Subway Map
题意:给出起点和终点,计算求出最短路径(最短路径即所经过的站点最少的),若最短路径不唯一,则选择其中换乘次数最少的一条线路. 思路:本题虽然也是求最短路径,但是此路径是不带权值的,路径长度即所经过的边 ...
- 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法
self是什么,就是一个函数,就是一个形式参数 4.self就是调用当前方法的对象 静态字段,公有属性 静态字段使用场景,每个对象中保存相同的东西时,可以使用静态字段,公有属性 5.封装的理解 类中封 ...
- php无刷新上传图片
1. 引入文件 <!--图片上传begin--> <script type="text/javascript" src="/js/jquery.form ...
- PL/SQL 训练03 --异常
--程序员在开发的时候,经常天真的认为这个世界是完美的,用户如同自己般聪明,总能按照自己设想的方式--操作系统输入数据.但残酷的事实告诉我们,这是不可能的事情,用户总会跟我们相反的方式操作系统--于是 ...
- WARNING: cell0 mapping not found - not syncing cell0
WARNING: cell0 mapping not found - not syncing cell0
- 「小程序JAVA实战」微信开发者工具helloworld(三)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-03/ 第一个小程序demo的运行,首选需要去使用开发工具 开发工具下载安装 https://mp. ...
- Hibernate XXX.hbm.xml 里的class标签的 schema 属性解释
转自:https://blog.csdn.net/mym43210/article/details/30230173 1 <?xml version="1.0" encodi ...