Tautology(POJ3295-构造法)
来源:互联网 发布:abp源码 编辑:程序博客网 时间:2024/06/01 12:53
Description
WFF 'N PROOF is a logic(逻辑的) game played withdice(骰子). Each die has six faces representing somesubset(子集) of the possiblesymbols(象征) K, A, N, C, E, p, q, r, s, t. A Well-formedformula(公式) (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.
- 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(意味), andequals as defined(定义) in the truth table below.
A tautology is a WFF that has value 1 (true) regardless(不管) of the values of itsvariables(变量). For example,ApNp is a tautology(同义反复) because it is true regardless of the value ofp. On the other hand, ApNq is not, because it has the value 0 forp=0, q=1.
You must determine whether or not a WFF is a tautology.
Input
Input(投入) consists of several test cases. Each test case is a single line containing a WFF with no more than 100symbols(象征). A line containing 0 follows the last case.
Output
For each test case, output(输出) a line containingtautology or not as appropriate.
Sample Input
ApNpApNq0
Sample Output
tautologynot
Source
刚开始没看懂题,懵逼了好一阵子。后来还是靠前辈们的博客的指点才得以顺利解题。大神们的代码通常很精炼,有着谈笑间樯橹灰飞烟灭的神韵。
算字符串WWF的值要从后往前解,因为每一个操作都是操作后面的表达式的,要想从前面开始必须先算出后面的表达式,所以从后面往前解才是正确的做法。
#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int MAXN = 120;int stack[MAXN];//数组实现堆栈char WWF[MAXN];int p, q, r, s, t;void DoIt()//用一个堆栈从字符串末尾进行操作{int top = 0;int len = strlen(WWF);for (int i = len - 1; i >= 0; i--){if (WWF[i] == 'p') stack[top++] = p;else if (WWF[i] == 'q') stack[top++] = q;else if (WWF[i] == 'r') stack[top++] = r;else if (WWF[i] == 's') stack[top++] = s;else if (WWF[i] == 't') stack[top++] = t;else if (WWF[i] == 'K'){int t1 = stack[--top];int t2 = stack[--top];stack[top++] = (t1&&t2);}else if (WWF[i] == 'A'){int t1 = stack[--top];int t2 = stack[--top];stack[top++] = (t1 || t2);}else if (WWF[i] == 'N'){int t1 = stack[--top];stack[top++] = (!t1);}else if (WWF[i] == 'C'){int t1 = stack[--top];int t2 = stack[--top];if (t1 == 1 && t2 == 0)stack[top++] = 0;else stack[top++] = 1;}else if (WWF[i] == 'E'){int t1 = stack[--top];int t2 = stack[--top];if (t1 == t2) stack[top++] = 1;else stack[top++] = 0;}}}bool solve()//p,q,r,s,t枚举所有可能取值{for (p = 0; p<2; p++)for (q = 0; q<2; q++)for (r = 0; r<2; r++)for (s = 0; s<2; s++)for (t = 0; t<2; t++){DoIt();if (stack[0] == 0)return false;}return true;}int main(){while (scanf("%s", &WWF)){if (strcmp(WWF, "0") == 0)break;if (solve())printf("tautology\n");//判断是否是永真式else printf("not\n");}return 0;}转载地址:http://www.cnblogs.com/kuangbin/archive/2012/08/13/2636855.html
参考博客:優YoU http://user.qzone.qq.com/289065406/blog/1309062835
- Tautology(POJ3295-构造法)
- poj3295 -- Tautology--构造法
- poj3295 Tautology(构造法)
- poj3295——Tautology(构造法)
- poj3295 Tautology —— 构造法
- POJ3295《Tautology》方法:构造栈
- poj3295 Tautology 构造 数据结构 枚举
- POJ3295 Tautology (模拟)
- poj3295(构造法?)
- (前缀表达式)poj3295 Tautology
- POJ3295--Tautology
- poj3295-Tautology
- POJ3295 Tautology
- poj3295 Tautology
- poj3295--Tautology
- POJ3295-Tautology
- POJ3295-Tautology
- Poj3295 Tautology
- CSS Mastery摘要(2)--Visual Formatting Model Overview
- Android移动开发-使用OpenGL来绘制3D纹理图的实现
- LeetCode-86-Partition List 链表
- h5编辑API之Range对象
- 智能小车34:汇编与C语言一起玩
- Tautology(POJ3295-构造法)
- 堆和栈的区别,以及申请方式,及效率的区别
- 让以及收敛的网络继续train下去
- Java300StudyNote(12)-Java浮点数以及浮点数误差问题、BigInteger、BigDecimal、float与long谁更大
- Trapping Rain Water
- JVM (PART X) 内存分配图解
- 读《大学之路》上册
- 计算机视觉资料大合集
- 链表(创建,插入,删除和打印输出