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, ...
随机推荐
- 【c3p0】目前使用它的开源项目有Hibernate,Spring等
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. c3p0与dbcp区别 JNDI ...
- CODEVS 3286 火柴排队
[题目描述 Description] 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为: ,其中 ai表示 ...
- 查看jdk的位数
public class Test { public static void main(String[] args) { System.out.println("bit of JVM is ...
- Eclipse导入git上的maven web项目 部署
1 Eclipse中导入Git的maven项目 方法1: (1)首先当然是拉代码. 在Eclipse里面有个Git Repositories Exploring.就是Git仓库,clone a git ...
- 一些简单的问题. 2的10次方与k (涉及到b k m的要用乘来解读)
2的10次方是k k就表示2的10次方 2的16次方,解读为 2的6次方(64)*2的10次方(k) 简写为64k 64k=64*k 同理2的20次方 解读为2的10次方*2的10次方 k ...
- cocos2d-html5 Layer 和 Scene 创建模式
var myLayer = cc.Layer.extend({ init:function() {//2 界面 var bRet = false; if (this._super()) { bRet ...
- HashMap与HashTable联系与区别
HashMap与HashTable 1.hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法. 2.hashTabl ...
- redis消息队列
http://blog.csdn.net/21aspnet/article/details/7455032
- Spring AOP实现方式二【附源码】
自动代理模式[和我们说的方式一 配置 和 测试调用不一样哦~~~] 纯POJO切面 源码结构: 1.首先我们新建一个接口,love 谈恋爱接口. package com.spring.aop; /* ...
- 坑爹的UICollectionView
最近用UICoolectionView的时候遇到一个很DT的问题,我往VC里加12个视图,结果显示成这样(右边是期待的样子): 研究了一下午,终于发现了问题: @interface FpL ...