字母: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 (构造题)的更多相关文章

  1. poj 3295 Tautology (构造)

    题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...

  2. POJ 3295 Tautology(构造法)

    题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  3. [ACM] POJ 3295 Tautology (构造)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 Descrip ...

  4. POJ 3295 Tautology 构造 难度:1

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9580   Accepted: 3640 Descrip ...

  5. 构造 + 离散数学、重言式 - POJ 3295 Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...

  6. POJ 3295 Tautology(构造法)

    http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...

  7. POJ 3295 Tautology (构造法)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7716   Accepted: 2935 Descrip ...

  8. poj 3295 Tautology(栈)

    题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...

  9. poj 3295 Tautology 伪递归

    题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...

随机推荐

  1. org.springframework.orm.jpa.JpaTransactionManager

    [第九章] Spring的事务 之 9.2 事务管理器 ——跟我学spring3 http://sishuok@com/forum/blogPost/list/0/2503.html

  2. css 垂直同步的几种方式

    第一种: 利用 display:table 和 display:table-cell 的方式 这种方式就好像将table布局搬过来一样,相信大家对table的td还是有印象的,它的内容是可以设置垂直居 ...

  3. Python属性、方法和类管理系列之----描述符类

    什么是描述符类? 根据鸭子模型理论,只要具有__get__方法的类就是描述符类. 如果一个类中具有__get__和__set__两个方法,那么就是数据描述符,. 如果一个类中只有__get__方法,那 ...

  4. 解决Deprecated: preg_replace(): The /e modifier is deprecated, use

    使用php5.5运行ecshop的时候出现如下错误Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace ...

  5. WLLCM这五个字母全排列数目

           经过训练的话一眼看出来是5!/2!:我想的是先排WLCM那么是4!,5个位置,由于L左右两边的位置其实是一样的(再插入的还是L),索以结果是4*4!,这样重复了,看下图.         ...

  6. PHP扩展编写示例

    1.生成描述文件,包含对函数等的定义 [chengyi@localhost php-extension]$ cat hello_cy.def string self_concat(string str ...

  7. uva 1298 - Triathlon

    半平面交的题: 这个题目的亮点就是建模: #include<cstdio> #include<algorithm> #include<cmath> #define ...

  8. easyui源码翻译1.32--panel(面板)

    前言 昨天发布了表格datagrid的翻译源码 ,easyui的许多插件有依赖关系  比如datagrid 的渲染需要panel.resizable.linkbutton.pagination   今 ...

  9. 浅谈HTTP中Get与Post的区别/HTTP协议与HTML表单(再谈GET与POST的区别)

    HTTP协议与HTML表单(再谈GET与POST的区别) GET方式在request-line中传送数据:POST方式在request-line及request-body中均可以传送数据. http: ...

  10. paip.无线路由器的无线接入WAN方式WDS设置大法

    paip.无线路由器的无线接入WAN方式WDS设置大法 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn. ...