POJ - 3295 - Tautology (构造)
2015-05-06 17:07
501 查看
题目传送:Tautology
思路:枚举所有变量可能的值(0或1),算出其表达式的值,因为题目是要求是否是永真式,求式子的真值可以用栈来求,栈的话,可以自己构造一个栈,也可以用STL的stack
AC代码:
思路:枚举所有变量可能的值(0或1),算出其表达式的值,因为题目是要求是否是永真式,求式子的真值可以用栈来求,栈的话,可以自己构造一个栈,也可以用STL的stack
AC代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <deque> #include <cctype> #define LL long long #define INF 0x7fffffff using namespace std; char str[105]; int p, q, r, s, t; int len; int fun() { stack<int> st; for(int i = len - 1; i >= 0; i --) { if(str[i] == 'p') st.push(p); else if(str[i] == 'q') st.push(q); else if(str[i] == 'r') st.push(r); else if(str[i] == 's') st.push(s); else if(str[i] == 't') st.push(t); else if(str[i] == 'K') { int t1 = st.top(); st.pop(); int t2 = st.top(); st.pop(); if(t1 == 1 && t2 == 1) st.push(1); else st.push(0); } else if(str[i] == 'A') { int t1 = st.top(); st.pop(); int t2 = st.top(); st.pop(); if(t1 == 0 && t2 == 0) st.push(0); else st.push(1); } else if(str[i] == 'N') { int t1 = st.top(); st.pop(); st.push(!t1); } else if(str[i] == 'C') { int t1 = st.top(); st.pop(); int t2 = st.top(); st.pop(); if(t1 == 1 && t2 == 0) st.push(0); else st.push(1); } else if(str[i] == 'E') { int t1 = st.top(); st.pop(); int t2 = st.top(); st.pop(); if((t1 == 1 && t2 == 1) || (t1 == 0 && t2 == 0)) st.push(1); else st.push(0); } } return st.top(); } int solve() { 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 ++) { int tmp = fun(); if(tmp == 0) return 0; } return 1; } int main() { while(scanf("%s", str) != EOF) { if(strcmp(str, "0") == 0) break; len = strlen(str); int flag = solve(); if(flag) { printf("tautology\n"); } else printf("not\n"); } return 0; }
相关文章推荐
- POJ 3295 Tautology 构造 难度:1
- POJ 3295 Tautology(构造)(栈)
- POJ 3295 Tautology 构造 stack
- POJ 3295 ——Tautology 枚举 构造 位运算可优化
- POJ 3295 Tautology 构造数列及STL中栈的运用
- poj 3295 Tautology (构造)
- [ACM] POJ 3295 Tautology (构造)
- POJ 3295 Tautology (构造题)
- POJ 3295 Tautology(递归、构造)
- poj_3295 Tautology(构造+枚举)
- POJ 3295 Tautology 构造方法
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
- POJ 3295 Tautology(枚举 + 构造)
- [ACM] POJ 3295 Tautology (构造)
- POJ 3295-Tautology(构造法+栈)
- POJ 3295-Tautology(模拟-逻辑表达式)
- poj 3295 Tautology (模拟栈操作+状压)
- POJ 3295 Tautology (栈模拟)
- poj 3295——Tautology
- ACM——POJ 3295 (Tautology)