POJ 3295 Tautology(构造法)
来源:互联网 发布:天猫耐克官网抢鞋软件 编辑:程序博客网 时间:2024/06/01 07:54
Time Limit: 1000MS Memory Limit: 65536K
题目地址:http://poj.org/problem?id=3295
Problem 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.
The meaning of a WFF is defined as follows:
- 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.
Definitions of K, A, N, C, and E
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
ApNp
ApNq
0
Sample Outputtautology
not
Source
Waterloo Local Contest, 2006.9.30
解题思路
问题中的K,A,N,C,E可以看作是运算符,p, q, r, s, t可以看作是运算的元素,它们之间的运算关系在表格中已经清晰地给出。这道题的标签是“构造”,意思应该是构造出栈来进行计算,这种方法也运用于括号匹配等问题。解题方案就是从后往前扫描一遍字符串,遇到小写字母就压栈,遇到大写字母就运算。(具体做法见代码注释)由于题目保证给出的字符串是WWF,所以最后一定可以保证栈中只剩一个元素。总的来说只要逻辑清晰,解决类似问题并不难。
AC代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<string>#include<queue>#include<sstream>#include<list>#include<stack>#define ll long long#define ull unsigned long long#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--)#define fil(a,b) memset((a),(b),sizeof(a))#define cl(a) fil(a,0)#define PI 3.1415927#define inf 0x3f3f3f3fusing namespace std;int p, q, r, s, t;string in;stack<int> ex;int judge(){ rrep(i, in.size() - 1, 0)//从后向前读字符串 { if (islower(in[i]))//元素入栈操作 { if (in[i] == 'p') ex.push(p); if (in[i] == 'q') ex.push(q); if (in[i] == 'r') ex.push(r); if (in[i] == 's') ex.push(s); if (in[i] == 't') ex.push(t); } else//运算操作 { if (in[i] == 'N') { if (ex.top()) { ex.pop(); ex.push(0); } else { ex.pop(); ex.push(1); } } if (in[i] == 'E') { int temp = ex.top();ex.pop(); if (ex.top() == temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0);} } if (in[i] == 'K') { int temp = ex.top();ex.pop(); if (ex.top() & temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } } if (in[i] == 'A') { int temp = ex.top();ex.pop(); if (ex.top() | temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } } if (in[i] == 'C') { int temp = ex.top();ex.pop(); if (ex.top() <= temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } } } } return ex.top();//返回0表示不是tautology}int main(void){ while (cin >> in && !(in[0] == '0'&&in.size() == 1)) { int flag = 1; rep(it, 0, 31)//由于只有5种运算元素,此处可枚举所有的32种情况 { while(!ex.empty()) ex.pop(); int i = it; p = i & 1;i >>= 1; q = i & 1;i >>= 1; r = i & 1;i >>= 1; s = i & 1;i >>= 1; t = i & 1; flag = judge(); if (!flag) break; } if (flag) cout << "tautology\n"; else cout << "not\n"; } return 0;}
2016 年 09月 21日
- 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 (构造)
- 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(构造法)
- (构造法)Tautology
- 机器学习基础--概率论与数理统计 chapter1 section5 条件概率
- sem_wait()和sem_timedwait()区别
- date_cal_bc
- Android开发板在Linux系统下的调试工具
- hdu 5759 2016 Multi-University Training Contest 3 Gardener Bo 解题报告
- POJ 3295 Tautology(构造法)
- 运用属性动画实现图标的竖直弹出
- JavaEE之位运算
- json和pickle区别
- Android6.0及以上版本一次请求多个权限的处理方式
- PID算法
- POJ 1049 Round and Round We Go 大数模拟
- 广州找工作 android工程师实习生 经验
- Codeforces Round 371 D Animals and Puzzle