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 ...
随机推荐
- 平均分割list
private static List<List<Integer>> splitList(List<Integer> lists,int limit){ int s ...
- HTTP/HTTPS原理详解
简介 HTTP(Hypertext Transfer Protocal,超文本传输协议)是WWW(World Wide Web,万维网)数据传输的基础,规定如何传输超文本.HTTP协议存在多个版本:H ...
- MATLAB01
在命令行窗口输入edit就会进入代码编写区,编写完毕点击运行后会先进行保存,然后再执行代码,保存时候一定要以英文开头. 数组 创建矩阵: 函数名 描述 zero(m,n) 创建m行n列全零矩阵 one ...
- 杂项:UN-标准通用置标语言
ylbtech-杂项:标准通用置标语言 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 ...
- mysql 常见参数
my.cnf[client] 对mysql的所有客端都生效的[mysql] 只对mysql这个命令有效了[mysqd][mysqld_multi] 多实例启动[mysqld_safe][mysqldN ...
- Lambda中的常用sql方法
1.Groupby 对集合进行分组,如: var dllList = _menuMan.Load(c => c.TXT_ASSEMBLYNAME != null).GroupBy(c=>c ...
- kettle init oracle jdbc
- 「小程序JAVA实战」小程序视图之细说数据绑定(13)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-13/ 在前面的小节里面其实对数据绑定都有所了解了,在这次给老铁在好好说下数据绑定,看下它的方方面面 ...
- Visual C++ Samples-------------Code Project
https://msdn.microsoft.com/en-us/library/hyds2fy1(v=vs.80).aspx
- Sequence(尺取)
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...