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

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的更多相关文章

  1. [POJ3295]Tautology

    [POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...

  2. POJ-3295 Tautology (构造)

    https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...

  3. 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)或 ...

  4. POJ3295 Tautology(枚举)

    题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...

  5. poj3295 Tautology , 计算表达式的值

    给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...

  6. POJ3295 Tautology(栈+枚举)

    Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...

  7. ACM学习历程——POJ3295 Tautology(搜索,二叉树)

    Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...

  8. POJ3295 Tautology 解题报告

    直接上分析: 首先 弄清各种大写字母的操作的实质 K 明显 是 and   & A 是 or      | N 是 not   ! C  由表格注意到 当 w<=x 时 值为1 E  当 ...

  9. POJ3295 Tautology重言式

    Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...

随机推荐

  1. 隐藏内容_网络推广_seo中级视频教程详解

    课程背景:SEO(Search Engine Optimization),汉译为搜索引擎优化.搜索引擎优化是一种利用搜索引擎的搜索规则来提高目的网站在有关搜索引擎内的排名的方式.SEO目的理解是:为网 ...

  2. JS 正则 获取URL参数

    function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  3. 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.

    一:使用jsonp格式, 如jquery中ajax请求参数   dataType:'JSONP'. <html> <head> <title>title</t ...

  4. centos6.5 最小化安装无法上网

    在VMware里装了个centos 6.5.  最小化安装后无法上网.在 google里找到答案 第一步:执行命令启动网卡 (最小化安装不是自动启动的) [root@localhost]# ifcon ...

  5. 一个汉字的ASCII编码&#12288;

    一个汉字的ASCII编码:  

  6. LeetCode之Single Number以及拓展

    Problem 1:一个数组中有一个数字a只出现一次,其他数字都出现了两次.请找出这个只出现一次的数字? 考察知识点:异或运算 思路:比如数字 b^b = 0     a^0 = a 因此,可以将数组 ...

  7. C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义

    在C语言编译中,经常会出现一些系统的错误,这些错误如果在编译的时候不能很好的“预见”,会使系统“崩溃”,常见的捕获错误函数有: errno #include<errno.h> 这个变量是程 ...

  8. linux eval命令

    eval 功能说明:重新运算求出参数的内容.语 法:eval [参数]补充说明:eval可读取一连串的参数,然后再依参数本身的特性来执行.参 数:参数不限数目,彼此之间用分号分开. 1.eval命令将 ...

  9. Oracle外部表的使用

    外部表可以像其它表一样,用select语句作查询.但不能做DML操作,不能建index,不接受约束.这是因为它不是以段的形式存于数据库中,只是以数据字典构造存在,指向一个或多个操作系统文件. 外部表的 ...

  10. 【Cardboard】 体验 - Google Cardboard DIY及完成后简单体验

    体验 - Google Cardboard DIY及完成后简单体验 今年的Google I/O最让我感兴趣的除了Material Design以外就是这个Google Cardboard了.据说是Go ...