zoukankan      html  css  js  c++  java
  • poj 3295 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 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

     

  • 相关阅读:
    jProfiler远程连接Linux监控jvm的运行状态
    Linux性能监控分析命令(五)—free命令介绍
    Linux性能监控分析命令(三)—iostat命令介绍
    Python编程练习题学习汇总
    OSPF与ACL综合应用实验
    ACL基础知识点总结
    NAT基础知识点
    VLAN实验五:利用三层交换机实现VLAN间路由
    VLAN实验四:利用单臂路由实现VLAN间路由
    VLAN实验三:理解Hybrid接口
  • 原文地址:https://www.cnblogs.com/cxbky/p/4915345.html
Copyright © 2011-2022 走看看