poj_3295 枚举

思路:枚举。

p,q,r,s,t只能是0或者1,共有32种情况,对于给定的一个WFF,从字符串末尾通过栈不断向前规约,遇到计算符号(K,A,N,C,E)从栈顶弹出相应的元素计算,结果再入栈。因为给定的case是WFF,所以最后只剩一个数字,弹出判断是否为1,当所有的情况都是1时,是tautology。 


<span style="font-size:18px;">#include <iostream>
#include<stack>

using namespace std;

char wff[110]; 
int p,q,r,s,t;
int last;
stack<int> ss;

bool isVariable(char value)
{
	switch(value)
	{
		case 'p':ss.push(p);return true;
		case 'q':ss.push(q);return true;
		case 'r':ss.push(r);return true;
		case 's':ss.push(s);return true;
		case 't':ss.push(t);return true;
	}
	return false;
}

void operate(char symbol)
{
	switch(symbol)
	{
		case 'K':
			{
				int x=ss.top();
				ss.pop();
				int y=ss.top();
				ss.pop();
				ss.push(x&&y);
				break;
			}
		case 'A':
			{
				int x=ss.top();
				ss.pop();
				int y=ss.top();
				ss.pop();
				ss.push(x||y);
				break;
			}
		case 'N':
			{
				int x=ss.top();
				ss.pop();
				ss.push(!x);
				break;
			}
		case 'C':
			{
				int x=ss.top();
				ss.pop();
				int y=ss.top();
				ss.pop();
				ss.push((!x)||y);
				break;
			}
		case 'E':
			{
				int x=ss.top();
				ss.pop();
				int y=ss.top();
				ss.pop();
				ss.push(x==y);
				break;
			}
	}
}

int main()
{
	while(cin>>wff && wff[0]!='0')
	{
		int len=strlen(wff);
		bool flag=true;
		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++)
						{
							for(last=len-1;last>=0;last--)
							{
								if(!isVariable(wff[last]))
								{
									operate(wff[last]);
								}
							}
							int ans=ss.top();
							ss.pop();
							if(!ans)
							{
								flag=false;
								break;
							}
						}
						if(!flag)
						{
							break;
						}
					}
					if(!flag)
					{
						break;
					}
				}
				if(!flag)
				{
					break;
				}
			}
			if(!flag)
			{
				break;
			}
		}
		if(flag)
		{
			cout<<"tautology"<<endl;
		}
		else
		{
			cout<<"not"<<endl;
		}
	}
	return 0;
	//system("pause");
}</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值