[POJ3295]Tautology
[POJ3295]Tautology
试题描述
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 |
A 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 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.
输出
For each test case, output a line containing tautology or not as appropriate.
输入示例
ApNp
ApNq
输出示例
tautology
not
数据规模及约定
见“输入”
题解
枚举 p, q, r, s, t 的值,然后带进去递归求出这个串的值,如果都为真那么就是“tautology”,否则是“not”。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; #define maxn 110
#define maxal 300
char S[maxn];
int ord[maxal];
bool val[10]; struct Info {
int p, v;
Info() {}
Info(int _, int __): p(_), v(__) {}
} ;
Info check(int l) {
if(islower(S[l])) return Info(l + 1, val[ord[S[l]]]);
if(S[l] == 'N') {
Info t;
t = check(l + 1);
return Info(t.p, t.v ^ 1);
}
if(S[l] == 'K') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v & t2.v);
}
if(S[l] == 'A') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v | t2.v);
}
if(S[l] == 'C') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, (t.v && !t2.v) ? 0 : 1);
}
if(S[l] == 'E') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v == t2.v);
}
return Info(0, 0);
} int main() {
ord['p'] = 0;
ord['q'] = 1;
ord['r'] = 2;
ord['s'] = 3;
ord['t'] = 4;
while(scanf("%s", S + 1) == 1) {
int all = (1 << 5) - 1, n = strlen(S + 1);
if(n == 1 && S[1] == '0') break;
bool ok = 1;
for(int i = 0; i <= all; i++) {
for(int j = 0; j < 5; j++)
val[j] = (i >> j & 1);
if(!check(1).v){ ok = 0; break; }
}
puts(ok ? "tautology" : "not");
} return 0;
}
[POJ3295]Tautology的更多相关文章
- 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 当 ...
- POJ3295 Tautology重言式
Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...
随机推荐
- Instant Python 中文缩减版
前言 本文主要来自<Python基础教程(第2版)>([挪]Magnus Lie Hetland著,司维 曾军崴 谭颖华译 人民邮电出版社) 中的“附录A 简明版本”,对于其中的有问题之处 ...
- 开发板ping不通主机和虚拟机的看过来(转载)!
前几天在做uboot下用tftp下载文件到开发板的实验时,为了能解决开发板ping不通主机和虚拟机的问题,可谓绞尽脑汁,正所谓久病成医,虽然为了这一小问题废了我那么长时间,但我在解决问题的同时也学到了 ...
- pyqt2_官网教程
https://pythonspot.com/en/pyqt4/ Articles You can find a collection of PyQT articles below. Applicat ...
- Java排序算法——冒泡排序
import java.util.Arrays; //================================================= // File Name : Bubble_S ...
- JavaScript 日历
效果图: <html> <head> <script language="javascript"> /*@ 解题思路: .计算本月有多少天(先要 ...
- Linux版MonoDevelop无法连接调试器的解决方案(Could not connet to the debugger)
安装了Linux版本的MonoDevelop之后,在运行程序的时候会提示Could not connnet to the debugger.的错误. 原因是新版本的Gnome Terminal不再接受 ...
- 【ZeroClipboard is not defined】的解决方法
参考:http://www.cnblogs.com/jfw10973/p/3921899.html https://github.com/zeroclipboard/zeroclipboard 近期该 ...
- tableview中在tableheaderView上放一个视图,第一次进入视图显示不正常,往下拉视图仍然不正常,往上拉视图正常
解决办法: frame来源不正常,从直接在viewDidLoad方法中设置的frame,改为 - (void)viewDidLayoutSubviews { [super viewDidLayoutS ...
- Android中怎么用this
在JAVA程序中似乎经常见到“this”,自己也偶尔用到它,但是到底“this”该怎么用,却心中无数!很多人一提起它,就说“当前对象”,可到底什么是当前对象,是什么当前对象,他自己也不清楚.现在让大家 ...
- C语言getopt()函数的使用
getopt(分析命令行参数) 相关函数表头文件 #include<unistd.h>定义函数 int getopt(int argc,char * ...