POJ 3295 Tautology (构造题)
2015-08-10 13:54
295 查看
字母:K, A, N, C, E 表示逻辑运算
字母:p, q, r, s, t 表示逻辑变量 0 或 1
给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not
枚举每个逻辑变量的值,5个变量,共2^5种情况,对于每种情况都为真则为永真式。
代码:
字母: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; }
相关文章推荐
- jquery 为动态生成元素绑定事件
- 优秀的博客
- 如何在 Ubuntu 上安装配置管理系统 Chef (大厨)
- jquery学习
- windows备份远程mysql数据库
- android 页面标题栏公用控件
- tarjan求点的双连通分量
- linux alias 用法
- fgetc,feof
- intent 不能从一个activity跳转到一个Fragment
- js关于for循环里的setTimeout
- A + B高精度
- Basic Calculator II
- appium的安装
- Path和ClassPath差异
- Axis2插件的安装以及代码生成
- MYSQL使用UTF8中文乱码终极解决办法
- Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听
- KMP-基础
- 电子开源社区