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

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(栈+枚举)的更多相关文章

  1. [POJ3295]Tautology

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

  2. POJ3295 Tautology(枚举)

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

  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

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

  5. POJ-3295 Tautology (构造)

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

  6. POJ3295 Tautology重言式

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

  7. poj 3295 Tautology(栈)

    题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...

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

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

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

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

随机推荐

  1. Avro之一:Avro简介

    一.引言 1. 简介 Avro是Hadoop中的一个子项目,也是Apache中一个独立的项目,Avro是一个基于二进制数据传输高性能的中间件.在Hadoop的其他项目中例如HBase(Ref)和Hiv ...

  2. python开发IO模型:阻塞&非阻塞&异步IO&多路复用&selectors

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  3. N卡控制面板把physx设置为cpu

    一般笔记本官方配置独显都会强于核显,若是独显不够强这里教你一个小技巧来缓解独显的压力:N卡控制面板把physx设置为cpu,可以试试看.对于不强大的独显有点效果.

  4. Deep Learning 学习笔记(3):Linear Regression 数据的预处理

    为了获得良好的收敛,在进行梯度下降前,我们可以对数据进行预处理. 目标是使得数据大小在同一个数据数量级上,均值为零. 一般将数据放缩到(-1,1)区间, 我们可以对数据进行如下操作: 其中u1是数据的 ...

  5. AE插件之SKYBOX CONVERTER

    AE插件之SKYBOX CONVERTER AE插件SKYBOX CONVERTER的主要作用是开发全景视频或者制作全景图片时,对添加的字幕.图片进行扭曲. 下载目录:http://www.gfxca ...

  6. Hadoop IO 特性详解(1)

    本文结合hadoop : the definitive guide精心而作,包含作者的心血,希望可以帮助大家理解一点hdfs的皮毛,足矣.(charles@xingbod.cn) hadoop本身自带 ...

  7. JanusGraph : 图和图数据库的简介

    JanusGraph:图数据库系统简介 图(graph)是<数据结构>课中第一次接触到的一个概念,它是一种用来描述现实世界中个体和个体之间网络关系的数据结构. 为了在计算机中存储图,< ...

  8. 22-从零玩转JavaWeb-代码块

    配套详解视频 局部代码块与初始化代码块 面向对象-静态代码块 代码块总结 组合关系与类的加载 静态代码块及字段初始化练习 一.什么是代码块 在类中或方法当中 使用{}括起来的一段代码 就称它是一个代码 ...

  9. mysql多个字段拼接

    Mysql的查询结果行字段拼接,可以用下面两个函数实现: 1. concat函数 mysql') from test ; +---------------------+ ') | +--------- ...

  10. Bootstrap教程目录

    1.Bootstrap 简介(Web前端CSS框架) 2.Bootstrap 学习资料 3.Bootstrap 入门 4.Bootstrap 概览 5.Bootstrap 栅格系统 6.Bootstr ...