码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 3295 - Tautology (构造)

时间:2015-05-06 17:43:05      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:acm   poj   构造   


题目传送: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 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 (构造)

标签:acm   poj   构造   

原文地址:http://blog.csdn.net/u014355480/article/details/45538109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!