码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3295 Tautology

时间:2015-10-27 21:42:58      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

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 Outputtautology

not

大致题意:

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

    其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;

    K、A、N、C、E为逻辑运算符,

    K --> and:  x && y

    A --> or:  x || y

    N --> not :  !x

    C --> implies :  (!x)||y

    E --> equals :  x==y

    问这个逻辑表达式是否为永真式。

分析:共 p,q,r,s,t 五个变量,也就是说最多共32种情况,分别进行枚举。

    如果表达式的值有结果为零,就输出 not,因为已经构不成永真式 

 

技术分享
  1 #include <iostream>
  2 #include <cstring>
  3 #include <stack>
  4 using namespace std;
  5 char str[150];
  6 stack<int>s;
  7 int p,q,r,ss,t; //各个逻辑变量的状态
  8 int wff;
  9 bool fun(char ch) //如果是逻辑变量则压栈
 10 {
 11     switch(ch)
 12     {
 13     case p:
 14         s.push(p);
 15         return true;
 16     case q:
 17         s.push(q);
 18         return true;
 19     case r:
 20         s.push(r);
 21         return true;
 22     case s:
 23         s.push(ss);
 24         return true;
 25     case t:
 26         s.push(t);
 27         return true;
 28     default:
 29         return false;
 30     }
 31 }
 32 void operators(char ch)
 33 {
 34     int a,b;
 35     switch(ch)
 36     {
 37     case K:
 38     {
 39         int a=s.top();
 40         s.pop();
 41         int b=s.top();
 42         s.pop();
 43         s.push(a&&b);
 44         break;
 45     }
 46     case A:
 47     {
 48         a=s.top();
 49         s.pop();
 50         b=s.top();
 51         s.pop();
 52         s.push(a||b);
 53         break;
 54     }
 55     case N:
 56     {
 57         a=s.top();
 58         s.pop();
 59         s.push(!a);
 60         break;
 61     }
 62     case C:
 63     {
 64         a=s.top();
 65         s.pop();
 66         b=s.top();
 67         s.pop();
 68         s.push((!a)||b);
 69         break;
 70     }
 71     case E:
 72     {
 73         a=s.top();
 74         s.pop();
 75         b=s.top();
 76         s.pop();
 77         s.push(a==b);
 78         break;
 79     }
 80     }
 81     return ;
 82 }
 83 int main()
 84 {
 85     while(cin>>str)
 86     {
 87         if(str[0]==0)
 88             break;
 89         int len=strlen(str)-1;
 90         bool flag=false;
 91         for(p=0; p<=1; p++)
 92         {
 93             for(q=0; q<=1; q++)
 94             {
 95                 for(r=0; r<=1; r++)
 96                 {
 97                     for(ss=0; ss<=1; ss++)
 98                     {
 99                         for(t=0; t<=1; t++)
100                         {
101                             for(wff=len; wff>=0; wff--)
102                             {
103                                 if(fun(str[wff])==0)
104                                     operators(str[wff]);
105                             }
106                             int ans=s.top(); //栈中最后一个元素
107                             s.pop();
108                             if(ans==0)
109                             {
110                                 flag=true;
111                             }
112                         }
113                         if(flag)
114                             break;
115                     }
116                     if(flag)
117                         break;
118                 }
119                 if(flag)
120                     break;
121             }
122             if(flag)
123                 break;
124         }
125         if(flag)
126             cout<<"not"<<endl;
127         else
128             cout<<"tautology"<<endl;
129     }
130     return 0;
131 }
View Code

 

 

 

poj 3295 Tautology

标签:

原文地址:http://www.cnblogs.com/cxbky/p/4915345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!