POJ - 3295 - Tautology (构造)
来源:互联网 发布:酷狗铃声截取软件 编辑:程序博客网 时间:2024/06/06 03:29
题目传送:Tautology
思路:枚举所有变量可能的值(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 0x7fffffffusing 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;}
0 0
- POJ - 3295 - Tautology (构造)
- POJ 3295 Tautology(构造)(栈)
- POJ 3295 Tautology(构造法 stack)
- POJ 3295 Tautology (构造法 栈)
- [ACM] 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(构造法)
- 初级->基本算法->构造法 poj 3295 Tautology(永真式)
- poj 3295 Tautology(经典构造算法题)
- POJ 3295-Tautology(构造法+栈)
- poj 3295 Tautology(枚举)
- Tautology(POJ 3295)(暴力枚举)
- ACM——POJ 3295 (Tautology)
- CentOs下mysql安装步骤详细
- 6 may l
- uva1374(暴力)
- Introducing SDN-MQ: A Powerful and Simple-to-Use Northbound Interface for OpenDaylight
- 第三章第二十九题
- POJ - 3295 - Tautology (构造)
- Android中基于HTTP的通信技术(2)使用Http的Post方式读取网络数据
- EAS-Web开发环境问题汇总
- IOS开发之block块详细分析
- 得到数据库中的数据,并在Highcharts控件中显示
- R语言基础备忘-plot()详解
- 闭包 Scala学习笔记-面向对象篇
- 黑马程序员--GUI
- 查询oracle被锁的表及其如何释放