POJ 3295-Tautology(构造法+栈)
来源:互联网 发布:敏感端口 编辑:程序博客网 时间:2024/06/05 20:57
Description
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (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, and equals as defined in the truth table below.
A tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=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 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
ApNpApNq0
Sample Output
tautologynot
题意:输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
K、A、N、C、E为逻辑运算符,
K --> and: x && y
A --> or: x || y
N --> not : !x
C --> implies : (!x)||y
E --> equals : x==y
问这个逻辑表达式是否为永真式。
思路:WFF的计算方法:从字符串WFF的末尾开始依次向前读取字符。构造一个栈stack,当遇到逻辑变量 p, q, r, s ,t 则将其当前的值压栈;遇到 N 则取栈顶元素进行非运算,运算结果的值压栈;遇到K, A, C, E则从栈顶中弹出两个元素进行相应的运算,将结果的值压栈。 由于输入是合法的,当字符串WFF扫描结束时,栈stack中只剩一个值,该值就是逻辑表达式WFF的值。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <stack>using namespace std;char str[110];stack<int>Q;int p,q,r,s,t,len;int IN_stack(){ int i; int x,y; for(i=strlen(str)-1; i>=0; i--) { if(str[i] == 'p') Q.push(p); else if(str[i] == 'q') Q.push(q); else if(str[i] == 'r') Q.push(r); else if(str[i] == 's') Q.push(s); else if(str[i] == 't') Q.push(t); else if(str[i]== 'K') { x = Q.top(); Q.pop(); y = Q.top(); Q.pop(); Q.push(x && y); } else if(str[i] == 'A') { x = Q.top(); Q.pop(); y = Q.top(); Q.pop(); Q.push(x || y); } else if(str[i] == 'N') { x = Q.top(); Q.pop(); Q.push(!x); } else if(str[i] == 'C') { x = Q.top(); Q.pop(); y = Q.top(); Q.pop(); Q.push((!x)||y); } else if(str[i] == 'E') { x = Q.top(); Q.pop(); y = Q.top(); Q.pop(); Q.push(x == y); } }}int judge(){ 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++) { IN_stack(); if(Q.top()==0) return 0; } return 1;}int main(){ while(~scanf("%s",str)) { getchar(); if(str[0] == '0') break; if(judge()) { printf("tautology\n"); } else printf("not\n"); } return 0;}
- POJ 3295 Tautology (构造法 栈)
- POJ 3295-Tautology(构造法+栈)
- poj 3295 Tautology(构造法)
- POJ 3295 Tautology(构造法)
- POJ 3295 Tautology(构造法 stack)
- POJ 3295 Tautology(构造法)
- POJ 3295 Tautology(构造)(栈)
- POJ 3295 Tautology 构造方法
- POJ - 3295 - Tautology (构造)
- POJ 3295 Tautology 构造 stack
- [POJ 3295]Tautology[构造][DFS]
- 初级->基本算法->构造法 poj 3295 Tautology(永真式)
- poj 3295 Tautology(经典构造算法题)
- [ACM] POJ 3295 Tautology (构造)
- POJ 3295 Tautology(递归、构造)
- POJ 3295 Tautology (栈模拟)
- poj3295 -- Tautology--构造法
- poj3295 Tautology(构造法)
- 新浪微博数据挖掘食谱之十四: 用户篇 (分析用户的粉丝和朋友)
- 将心比心,你的人生道路才会更宽广
- 【UNIX】内核对内核空间内存的管理以及对用户进程的堆内存分布
- NSDate比较
- IOS 开发学习一 基础知识
- POJ 3295-Tautology(构造法+栈)
- Sublime Text 3 快捷键汇总
- LeetCode OJ 之 Search a 2D Matrix (二维矩阵的搜索)
- 标准IO之格式化输入和输出
- UML画图—机房收费(一)
- tableView跳转界面时取消之前界面被选中状态
- 2014,2015
- 关于C和C++中void mian()和int main()的问题
- XML约束之DTD