Tautology---poj3295(枚举判断是否为永真式)

题目链接:http://poj.org/problem?id=3295

题意:判断是否是永真式,其中 p q r s t 表示逻辑变量其值为0或者1;

枚举所有逻辑变量的值,然后判断是否出现false

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>
#include<stack> using namespace std; #define met(a, b) memset(a, b, sizeof(a))
#define N 111 typedef long long LL; int p, q, r, s, t, len;
char str[N]; bool Judge()
{
stack<int> Sta;
for(int i=len-; i>=; i--)
{
int x1, x2;
if(str[i] == 'p')Sta.push(p);
else if(str[i] == 'q')Sta.push(q);
else if(str[i] == 'r')Sta.push(r);
else if(str[i] == 's')Sta.push(s);
else if(str[i] == 't')Sta.push(t);
else if(str[i] == 'K')
{
x1 = Sta.top();Sta.pop();
x2 = Sta.top();Sta.pop();
Sta.push(x1&&x2);
}
else if(str[i] == 'A')
{
x1 = Sta.top();Sta.pop();
x2 = Sta.top();Sta.pop();
Sta.push(x1||x2);
}
else if(str[i] == 'N')
{
x1 = Sta.top();Sta.pop();
Sta.push(!x1);
}
else if(str[i] == 'C')
{
x1 = Sta.top();Sta.pop();
x2 = Sta.top();Sta.pop();
Sta.push( !(x1&&!x2) );
}
else
{
x1 = Sta.top();Sta.pop();
x2 = Sta.top();Sta.pop();
Sta.push( x1==x2 );
}
}
return Sta.top()==;
} bool solve()
{
len = strlen(str); for(p=; p<; p++)
for(q=; q<; q++)
for(r=; r<; r++)
for(s=; s<; s++)
for(t=; t<; t++)
if(!Judge())
return false;
return true;
} int main()
{
while(scanf("%s", str), strcmp(str, ""))
{
if(solve())puts("tautology");
else puts("not");
}
return ;
}
上一篇:POJ2318TOYS(叉积判断点与直线位置)


下一篇:Url地址重写