POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算
字母:p, q, r, s, t 表示逻辑变量 0 或 1
给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not
枚举每个逻辑变量的值,5个变量,共2^5种情况,对于每种情况都为真则为永真式。
代码:
/***************************************
Problem: 3295 User:
Memory: 688K Time: 0MS
Language: G++ Result: Accepted
***************************************/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack> using namespace std; int p, q, r, s, t; // variables 0 or 1 int st[32][5] = {0,0,0,0,0, 0,0,0,0,1, 0,0,0,1,0, 0,0,0,1,1, 0,0,1,0,0, 0,0,1,0,1, 0,0,1,1,0,
0,0,1,1,1, 0,1,0,0,0, 0,1,0,0,1, 0,1,0,1,0, 0,1,0,1,1, 0,1,1,0,0, 0,1,1,0,1,
0,1,1,1,0, 0,1,1,1,1, 1,0,0,0,0, 1,0,0,0,1, 1,0,0,1,0, 1,0,0,1,1, 1,0,1,0,0,
1,0,1,0,1, 1,0,1,1,0, 1,0,1,1,1, 1,1,0,0,0, 1,1,0,0,1, 1,1,0,1,0, 1,1,0,1,1,
1,1,1,0,0, 1,1,1,0,1, 1,1,1,1,0, 1,1,1,1,1};
char exp[105]; int get_value(char ch)
{
switch(ch) {
case 'p': return p;
case 'q': return q;
case 'r': return r;
case 's': return s;
case 't': return t;
case 'N': return -1;
default: return -2;
}
} int WFF(char ch, int a, int b)
{
if (ch == 'K') return a && b;
if (ch == 'A') return a || b;
if (ch == 'C') return (!b) || a;
if (ch == 'E') return a == b;
} bool solve()
{
int i, a, b;
int len = strlen(exp);
stack<int> mystack;
for (i = len - 1; i >= 0; --i) {
if (get_value(exp[i]) >= 0) {
a = get_value(exp[i]);
mystack.push(a);
} else if (get_value(exp[i]) == -1) {
a = mystack.top(); mystack.pop();
a = !a;
mystack.push(a);
} else {
a = mystack.top(); mystack.pop();
b = mystack.top(); mystack.pop();
a = WFF(exp[i], b, a);
mystack.push(a);
}
}
return mystack.top();
} int main()
{
int i;
while (scanf("%s", exp) != EOF) {
if (exp[0] == '0') break;
for (i = 0; i < 32; ++i) {
p = st[i][0]; q = st[i][1]; r = st[i][2];
s = st[i][3]; t = st[i][4];
if (!solve()) break;
}
if (i == 32) puts("tautology");
else puts("not");
}
return 0;
}
POJ 3295 Tautology (构造题)的更多相关文章
- poj 3295 Tautology (构造)
题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
随机推荐
- 转载 -- iOS数据持久化存储
作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方 ...
- Xcode 调试技巧-b
随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...
- Hadoop 2.4.0完全分布式平台搭建、配置、安装
一:系统安装与配置 Hadoop选择下载2.4.0 http://hadoop.apache.org / http://mirror.bit.edu.cn/apache/hadoop/common/h ...
- linux 安装Tesseract-OCR
linux 安装Tesseract-OCR 准备工作:一.编译环境: 1. gcc gcc-c++ make(这个环境一般机器都具备,可以忽略) yum install gcc gcc-c++ mak ...
- JSON漫谈
JSON: JavaScript Object Notation(JavaScript 对象表示法),JSON 是存储和交换文本信息的语法.类似 XML.JSON 比 XML 更小.更快,更易解析. ...
- Codeforces Round #210
A:简单题: #include<cstdio> using namespace std; int n,k; int main() { scanf("%d%d",& ...
- 146. LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- Android java.net.SocketException四大异常解决方案
java.net.SocketException如何才能更好的使用呢?这个就需要我们先要了解有关这个语言的相关问题.希望大家有所帮助.那么我们就来看看有关java.net.SocketExceptio ...
- dubbo spring2.5.6与spring 3冲突解决
dubbo的详细资料请参考: http://alibaba.github.io/dubbo-doc-static/Administrator+Guide-zh.htm#AdministratorGui ...
- WCF - Self Hosting
WCF - Self Hosting Here, the WCF service is hosted in a console application. Given below is the proc ...