POJ - 3295 - Tautology (构造)


题目传送: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;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值