Tautology

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

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int MAXN=105;
char a[MAXN];
int n,p,q,r,s,t;
bool ans;
bool isc(char c) {
return c=='p'||c=='q'||c=='r'||c=='s'||c=='t';
}
void push(char c,stack<int>&st) {
if(c=='p')st.push(p);
if(c=='q')st.push(q);
if(c=='r')st.push(r);
if(c=='s')st.push(s);
if(c=='t')st.push(t);
}
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);
}
void calc(char c,stack<int>&st) {
if(c=='K') {
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(K(a,b));
}
if(c=='A'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(A(a,b));
}
if(c=='N'){
int a;
a=st.top(),st.pop();
st.push(N(a));
}
if(c=='C'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(C(a,b));
}
if(c=='E'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(E(a,b));
}
}
int main() {
while(scanf("%s",a+1)!=EOF)
{
if(!strcmp(a+1,"0"))return 0;
n=strlen(a+1);
ans=true;
for(p=0; p<=1; ++p) {
for(q=0; q<=1; ++q) {
for(r=0; r<=1; ++r) {
for(s=0; s<=1; ++s) {
for(t=0; t<=1; ++t) {
stack<int>st;
for(int i=n; i>=1; --i) {
if(isc(a[i]))push(a[i],st);
else calc(a[i],st);
}
if(!st.top())
{
ans=false;
goto end;
}
}
}
}
}
}
end:{ans?puts("tautology"):puts("not");}
}
}

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

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

  5. POJ3295 Tautology(枚举)

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

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

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

  7. POJ3295 Tautology(栈+枚举)

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

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

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

  9. POJ3295 Tautology 解题报告

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

随机推荐

  1. 498. (leetcode)对角线遍历

    498. 对角线遍历 根据题目的图像看,主要有两种走法,第一种是向右上(顺时针方向),第二种是向左下(逆时针)走 我们设 x ,y初始为0,分别对应横纵坐标 现在分析右上(0,2) 为例:(注意右上的 ...

  2. Django学习 之 Django安装与一个简单的实例认识

    一.Django简介 1.MVC与MTV模型 (1)MVC模型 Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的. ...

  3. 本周总结(19年暑假)—— Part7

    日期:2019.8.25 博客期:113 星期日

  4. Java程序员所需要掌握的核心知识

    [Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. https://javaguide.cn/ 推荐使用 https://snailclimb.gitee.io/javag ...

  5. 多iframe应用引起的内存问题

    1. 内存泄漏 1.1 什么是内存泄漏? 程序的运行需要内存.只要程序提出要求,操作系统或者运行时(runtime)就必须供给内存. 对于持续运行的服务进程(daemon),必须及时释放不再用到的内存 ...

  6. 【剑指Offer面试编程题】题目1505:两个链表的第一个公共结点--九度OJ

    题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表 ...

  7. jqGrid一次性读取本地数据

    参考:http://blog.sina.com.cn/s/blog_54da57aa010154r7.html

  8. 第1节 storm编程:9、storm与kafka的整合

    详见代码. 下图,为设置kafka的首次消费策略,即首次消费的偏移量的示例:

  9. django中添加日志功能

    官方文档 猛戳这里 在settings中配置以下代码 #LOGGING_DIR 日志文件存放目录 LOGGING_DIR = "logs" # 日志存放路径 if not os.p ...

  10. 机器学习、深度学习中的信息熵、相对熵(KL散度)、交叉熵、条件熵

    信息熵 信息量和信息熵的概念最早是出现在通信理论中的,其概念最早是由信息论鼻祖香农在其经典著作<A Mathematical Theory of Communication>中提出的.如今 ...