POJ3295——Tautology

Tautology

Description

WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:

  • p, q, r, s, and t are WFFs
  • if w is a WFF, Nw is a WFF
  • if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.

The meaning of a WFF is defined as follows:

  • p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
  • K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E
     w  x   Kwx   Awx    Nw   Cwx   Ewx
  1  1   1   1    0   1   1
  1  0   0   1    0   0   0
  0  1   0   1    1   1   0
  0  0   0   0    1   1   1

tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.

You must determine whether or not a WFF is a tautology.

Input

Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

Output

For each test case, output a line containing tautology or not as appropriate.

Sample Input

ApNp
ApNq
0

Sample Output

tautology
not 题目大意:逻辑表达式求知,K, A, N, C, E为逻辑运算符,p, q, r, s, and t 为真假值。
      Kxy -> x&&y
      Axy -> x||y
      Nx -> !x
      Cxy -> x||(!y)
      Exy -> x==y
      判断是否表达式恒为真。
解题思路: 数值变量总共就5个,枚举这五个变量的值,有32种情况。
      处理字符串的时候,类似于逆波兰表达式的求值过程。
      从(S.length()-1)->0遍历,遇到小写字母将其对应的布尔值存入栈。
      遇到大写字母(除N外) 去除栈顶2个元素进行处理,后存入栈。
      遇到N,去除栈顶一个元素,取反后存入栈。
      遍历完返回S.top()。
Code:
 #include<iostream>
#include<string>
#include<stack>
using namespace std;
int q,p,s,r,t;
bool Is_Tau(string S)
{
int len=S.length()-,i,t1,t2;
stack<char> ST;
for (i=len;i>=;i--)
{
if (S[i]=='q') ST.push(q);
if (S[i]=='p') ST.push(p);
if (S[i]=='r') ST.push(r);
if (S[i]=='s') ST.push(s);
if (S[i]=='t') ST.push(t);
if (S[i]=='K')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1&&t2);
}
if (S[i]=='A')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1||t2);
}
if (S[i]=='C')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1||(!t2));
}
if (S[i]=='E')
{
t1=ST.top();
ST.pop();
t2=ST.top();
ST.pop();
ST.push(t1==t2);
}
if (S[i]=='N')
{
t1=ST.top();
ST.pop();
ST.push(!t1);
}
}
return ST.top();
}
int main()
{
string WFF;
while (cin>>WFF)
{
int OK=;
if (WFF=="") break;
for (q=; q<=; q++)
for (p=; p<=; p++)
for (r=; r<=; r++)
for (s=; s<=; s++)
for (t=; t<=; t++)
if (!Is_Tau(WFF))
{
OK=;
break;
}
if (OK) printf("tautology\n");
else printf("not\n");
}
return ;
}
上一篇:如何通过 IntelliJ IDEA 来提升 Java8 Stream 的编码效率


下一篇:在Debian 6上安装Gearman PHP扩展