思路:枚举。
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>