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,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
随机推荐
- will VS be going to
1.Future - Will vs. Going to 2."Will be doing" vs. "will do" 3.Simple Future
- linux下进入root
baoyu@ubuntu:~$ sudo password root sudo: password: command not found baoyu@ubuntu:~$ sudo passwd roo ...
- dom4j api 详解--XPath 节点详解
dom4j api 详解 http://871421448.iteye.com/blog/1546955 XPath 节点 http://www.w3school.com.cn/xpath/xpath ...
- TreeView递归取值
string jingyuan = ""; string jinghui = ""; private void DiGui(TreeNode tn) { if ...
- DEV GridControl表格数据源为空在表格中间显示提醒字符
private static void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.Custo ...
- java 中的匿名内部类
转自http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html 匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能 ...
- SPI协议及其工作原理详解
一.概述. SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控 ...
- Python 信号量
信号的概念 信号(signal)-- 进程之间通讯的方式,是一种软件中断.一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号. 几个常用信号: SIGINT 终止进程 中断进 ...
- Thinkcmf 在新浪云上的部署问题
最近要开发一个社团主页,于是想到了CMF内容管理系统的,但是直接在自己的服务器测试成本太高,于是选择了在新浪云上进行部署测试. 但是在安装Thinkcmf的过程中产生了一些技术性的问题.但最后终于在自 ...
- android 注销
1.在个人中心退出系统MainActivity 2.清空保存的登录数据 3.打开登录LoginActivity 方法: SharedPreferencesManager.getInstance(mCo ...