您的位置:首页 > 其它

POJ - 3295 - Tautology (构造)

2015-05-06 17:07 501 查看
题目传送: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: