POJ3295 Tautology(枚举)
题目链接。
分析:
最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <stack> using namespace std; const int maxn = + ; stack<bool> S;
char s[maxn];
bool hash[], ans; void check(char pos) { if(s[pos] == 'N') {
bool x = S.top(); S.pop();
S.push(!x);
} else if(s[pos] >= 'p' && s[pos] <= 't') {
S.push(hash[s[pos]]);
} else {
bool x, y;
x = S.top(); S.pop();
y = S.top(); S.pop(); if(s[pos] == 'K') S.push(x&y);
else if(s[pos] == 'A') S.push(x|y);
else if(s[pos] == 'C') S.push(!x | y); //即蕴含
else if(s[pos] == 'E') S.push(!(x^y));
} } int main() {
//freopen("my.txt", "r", stdin);
while(scanf("%s", s) == ) {
if(s[] == '') break;
ans = true;
int i; for(i=; i<(<<); i++) {
hash['p'] = i & ;
hash['q'] = i & (<<);
hash['r'] = i & (<<);
hash['s'] = i & (<<);
hash['t'] = i & (<<); int pos = strlen(s)-; while(pos >= ) {
check(pos);
pos--;
} ans = S.top(); S.pop(); if(ans == false) { printf("not\n"); break; }
} if(i >= (<<)) printf("tautology\n");
} return ;
}
另一种写法
该写法参考自Disuss:http://poj.org/showmessage?message_id=168123(里面对于莫名其妙的错误还有讲解,不错)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <stack> using namespace std; const int maxn = + ; char s[maxn];
int c_s, value; bool getval() {
bool t1, t2; switch(s[c_s++]) {
case 'p': return (value & (<<));
case 'q': return (value & (<<));
case 'r': return (value & (<<));
case 's': return (value & (<<));
case 't': return (value & (<<)); case 'N': return !getval();
case 'K': t1 = getval(); t2 = getval(); return t1 & t2;
case 'A': t1 = getval(); t2 = getval(); return t1 | t2;
case 'C': t1 = getval(); t2 = getval(); return (!t1 | t2);
case 'E': t1 = getval(); t2 = getval(); return (!(t1^t2));
}
} int main() {
// freopen("my.txt", "r", stdin);
while(scanf("%s", s) == ) {
if(s[] == '') break; for(value = ; value < (<<); value++){
c_s = ;
if(!getval()) { printf("not\n"); break; }
} if(value >= (<<)) printf("tautology\n");
} return ;
}
POJ3295 Tautology(枚举)的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- 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 ...
- POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...
- POJ3295 Tautology重言式
Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- 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 当 ...
随机推荐
- Android--图片的三级缓存策略
三级缓存缓存(内存)--->本地磁盘---->网络1.首先看一下图片存储到本地磁盘 public class FileUtils { String path;//文件存储的地方 publi ...
- 老鸟的Python新手教程
重要说明 这不是给编程新手准备的教程,假设您入行编程不久,或者还没有使用过1到2门编程语言,请移步!这是有一定编程经验的人准备的.最好是熟知Java或C,懂得命令行,Shell等.总之,这是面向老鸟的 ...
- CreateFont具体解释
CFont * f; f = new CFont; f->CreateFont(10, // nHeight 0, // nWidth 0, // n ...
- 【AIX】AIX 6.1 “C compiler cc is not found”问题的解决方案
一.问题的由来 前几天在AIX中安装部署 nginx-1.4.1,报如下错误: # cd nginx-1.4.1 # ./configure checking for OS + AIX 1 0004 ...
- C++之智能指针
导读 一直对智能指针有一种神秘的赶脚,虽然平时没怎么用上智能指针,也就看过STL中的其中一种智能指针auto_ptr,但是一直好奇智能指针的设计因此,今天看了一下<C++ Primer Plus ...
- Android四大图片缓存(Imageloader,Picasso,Glide,Fresco)原理、特性对比
四大图片缓存基本信息 Universal ImageLoader 是很早开源的图片缓存,在早期被很多应用使用. Picasso 是 Square 开源的项目,且他的主导者是 JakeWharton,所 ...
- Ubuntu下全命令行安装Android SDK
为了在AWS云服务器上实现自动化打包Android APP的APK包,我需要远程命令行环境下安装Android SDK,当然还要用代理或者科学上网,这里简单整理一下过程: 首先,由于墙的原因,Andr ...
- POJ 1113 Wall 求凸包的两种方法
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31199 Accepted: 10521 Descriptio ...
- HighCharts基本用法
var options={ chart: {type: 'column',renderTo: 'ChartDesigner1'},//type :图表类型(柱状图,饼状图),renderTo :指向页 ...
- js的new操作符
1.创建一个空对象,并且 this 变量引用该对象,同时还继承了该函数的原型. 2.属性和方法被加入到 this 引用的对象中. 3.新创建的对象由 this 所引用,并且最后隐式的返回 this . ...