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开发艺术探索》读书笔记 (4) 第4章 View的工作原理
本节和<Android群英传>中的第3章Android控件架构与自定义控件详解有关系,建议先阅读该章的总结 第4章 View的工作原理 4.1 初始ViewRoot和DecorView ( ...
- 9.14noip模拟试题
中文题目名称 祖孙询问 比赛 数字 英文题目名称 tree mat num 可执行文件名 tree mat num 输入文件名 tree.in mat.in num.in 输出文件名 tree.out ...
- C#错误异常列表
Exception: 所有异常对象的基类. SystemException:运行时产生的所有错误的基类. IndexOutOfRangeException:当一个数组的下标超出范围时运行时引发. Nu ...
- Android-adb相关
最近做android开发遇到无法通过usb链接设备的情况,通过wifi连接设备调试也颇为方便 1.android 要root , 下载终端app 比如 BetterTerminal 2.通过以下命令 ...
- 使用PPRevealSideViewController实现侧滑效果
使用起来还是比较简单的, 主要是几个步骤 AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchi ...
- Hibernate 一对多单向关联Demo
以Classes[班级]和Student[学生]为例的Demo Classes .java public class Classes implements Serializable { private ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- 【转】K短路
K短路 用dijsktra+A*启发式搜索 当点v第K次出堆的时候,这时候求得的路径是k短路.A*算法有一个启发式函数f(p)=g(p)+h(p), 即评估函数=当前值+当前位置到终点的最短距离g(p ...
- 优化HTTP前端请求构建高性能ASP.NET站点
HTTP请求的优化 在一个网页的请求过程中,其实整个页面的html结构(就是页面的那些html骨架)请求的时间是很短的,一般是占整个页面的请求时间的10%-20%.在页面加载的其余的时间实际上就是在 ...
- ASP.Net MVC4排序检索分页的实现
前言 上一节我们做到了如下的一个基础查询页面.本节我们向这个页面中加入排序.搜索和分页功能. 排序 从上图中的地址栏中可以看到这个页面调用的是Company Controller下的Index Act ...