POJ 3295 Tautology(构造法)

来源:互联网 发布:淘宝详情页价格说明 编辑:程序博客网 时间:2024/05/24 06:25
Tautology
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10004 Accepted: 3794

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

ApNpApNq0

Sample Output

tautologynot

输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,

其中p、q、r、s、t的值为1或0,K、A、N、C、E为逻辑运算符

K ~   x && y A ~   x || y N ~   !x C ~  (!x)||y E ~  x==y

问这个逻辑表达式是否为永真式,否输出not

p,q,r,s,e共有32种组合方式,枚举出来判断只要有一个式子不是永真,就输出not

#include <iostream>#include <cstdio>#include <cstring>#define N 110using namespace std;char ss[N];int a[N];int top;int p,q,r,s,t;bool tf(){    int len=strlen(ss);    top=0;    for(int i=len-1;i>=0;i--)    {        if(ss[i]=='p') a[top++]=p;        else  if(ss[i]=='q') a[top++]=q;         else  if(ss[i]=='r') a[top++]=r;          else  if(ss[i]=='s') a[top++]=s;           else  if(ss[i]=='t') a[top++]=t;           else if(ss[i]=='K')           {               int w=a[--top];               int e=a[--top];               a[top++]=(w&&e);           }           else if(ss[i]=='A')           {               int w=a[--top];               int e=a[--top];               a[top++]=(w||e);           }            else if(ss[i]=='N')           {               int w=a[--top];               a[top++]=(!w);           }            else if(ss[i]=='C')           {               int w=a[--top];               int e=a[--top];               a[top++]=(!w)||e;           }             else if(ss[i]=='E')           {               int w=a[--top];               int e=a[--top];               if(w==e) a[top++]=1;               else a[top++]=0;           }    }    if(!a[0]) return false;    else return true;}bool judge(){    for(p=0; p<=1; p++)        for(q=0; q<=1; q++)            for(r=0; r<=1; r++)                for(s=0; s<=1; s++)                    for(t=0; t<=1; t++)                    {                        if(!tf()) return false;                    }    return true;}int main(){    while(~scanf("%s",ss))    {        if(strcmp(ss,"0")==0)            break;        if(judge())            printf("tautology\n");        else printf("not\n");    }    return 0;}



0 0