POJ3295 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重言式的更多相关文章
- [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
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- 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 当 ...
随机推荐
- 浅谈区块链和p2p网络
最近对区块链产生了兴趣就去了解了一下,分享一下.... 首先要先了解一下什么叫做区块链: 区块链:简单来说就是一种基于分布式数据存储.点对点传输.共识机制.加密算法等计算机技术的新型应用模式. 相信说 ...
- electron-edge-js 环境搭建
确保nodejs环境 为 10.X (因为edge的编译需要node对应版本的支持,太新的node不包含对应edge的编译) 1.创建工程2.使用npm init初始化程序信息3.使用npm ins ...
- 使用package.json安装模块
node.js模块的安装可以使用npm安装,如下: $ npm install <Module Name> 每个项目的根目录下面,一般都需要一个package.json文件,定义了这个项目 ...
- SRS源码——Listener
1. 整理了一下Listener相关的UML类图:
- Django 学习组件分页器与自定制分页器
一.Django 分页器 1.django的分页器基础版 (1)首先是基础数据分别为 from django.db import models # Create your models here. c ...
- uniGUI之uniColorButton(21)
uniColorButton相当于VCL的ColorDialog1 若Palette 为空,则为系统自带颜色.
- 说说maven依赖冲突,依赖调解,依赖传递和依赖范围
说maven依赖冲突之前需要先说说maven的 依赖传递. 依赖传递 当前项目引入了一个依赖,该依赖的依赖也会被引入项目.更加准确的说法是,maven会解析直接依赖的POM,将那些必要的间接依赖,以传 ...
- 在WAMP环境下搭建ZendDebugger php调试工具的方法
东西不是新货,所以介绍就不做介绍了,下面主要是配置流程. 首先,下载ZendDebugger,下载链接:http://downloads.zend.com/pdt/server-debugger/,因 ...
- 首款中文渗透测试专用Linux系统—MagicBox
1. MagicBox的介绍 首款中文渗透测试专用Linux系统——MagicBox即将问世,中文名称:“魔方系统”,开发代号:Genesis.第一版本发布时间计划为2012年12月5日 这是 ...
- 记录一次Git远程仓库版本回退
操作过程: 首先查看远程仓库版本,如下图所见,最近一次提交为2018-03-19 22:16:25 第一步:使用git log命令查看历史提交记录,选择要回退的版本号,commit后面一串字符,这里我 ...