zoukankan      html  css  js  c++  java
  • poj 3295 -- Tautology

    Tautology
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9075   Accepted: 3474

    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

    A 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

    说是构造法。。哎!参考别人的思路,学习了一下。。。 http://blog.csdn.net/lyy289065406/article/details/6642766

      1 /*======================================================================
      2  *           Author :   kevin
      3  *         Filename :   Tautology.cpp
      4  *       Creat time :   2014-05-19 20:35
      5  *      Description :
      6  ========================================================================*/
      7 #include <iostream>
      8 #include <algorithm>
      9 #include <cstdio>
     10 #include <cstring>
     11 #include <queue>
     12 #include <cmath>
     13 #include <stack>
     14 #define clr(a,b) memset(a,b,sizeof(a))
     15 #define M 115
     16 using namespace std;
     17 char str[M];
     18 stack<int>st;
     19 int p,q,r,s,t,len;
     20 bool judgevalue(char c)
     21 {
     22     if(c == 'p')       st.push(p);
     23     else if(c == 'q')  st.push(q);
     24     else if(c == 'r')  st.push(r);
     25     else if(c == 's')  st.push(s);
     26     else if(c == 't')  st.push(t);
     27     else return false;
     28     return true;
     29 }
     30 void opera(char c)
     31 {
     32     if(c == 'K'){
     33         int x = st.top();
     34         st.pop();
     35         int y = st.top();
     36         st.pop();
     37         st.push(x && y);
     38     }
     39     else if(c == 'A'){
     40         int x = st.top();
     41         st.pop();
     42         int y = st.top();
     43         st.pop();
     44         st.push(x || y);
     45     }
     46     else if(c == 'C'){
     47         int x = st.top();
     48         st.pop();
     49         int y = st.top();
     50         st.pop();
     51         st.push((!x)||y);
     52     }
     53     else if(c == 'E'){
     54         int x = st.top();
     55         st.pop();
     56         int y = st.top();
     57         st.pop();
     58         st.push(x == y);
     59     }
     60     else if(c == 'N'){
     61         int x = st.top();
     62         st.pop();
     63         st.push(!x);
     64     }
     65     return;
     66 }
     67 bool slove()
     68 {
     69     bool flag = true;
     70     for(p = 0; p < 2; p++){
     71         for(q = 0; q < 2; q++){
     72             for(r = 0; r < 2; r++){
     73                 for(s = 0; s < 2; s++){
     74                     for(t = 0; t < 2; t++){
     75                         for(int ii = len-1; ii >= 0 ; ii--){
     76                             if(!judgevalue(str[ii])){
     77                                 opera(str[ii]);
     78                             }
     79                         }
     80                         int ans = st.top();
     81                         st.pop();
     82                         if(!ans){
     83                             flag = false;
     84                             break;
     85                         }
     86                     }
     87                     if(!flag) break;
     88                 }
     89                 if(!flag) break;
     90             }
     91             if(!flag) break;
     92         }
     93         if(!flag) break;
     94     }
     95     if(flag) return true;
     96     return false;
     97 }
     98 int main(int argc,char *argv[])
     99 {
    100     while(scanf("%s",str)!=EOF){
    101         getchar();
    102         if(str[0] == '0') break;
    103         len = strlen(str);
    104         if(slove()){
    105             printf("tautology
    ");
    106         }
    107         else printf("not
    ");
    108         clr(str,0);
    109     }
    110     return 0;
    111 }
    View Code
    Do one thing , and do it well !
  • 相关阅读:
    C#_WinForm接收命令行参数
    SQLite不支持的SQL语法总结
    Thirft框架介绍
    jquery获取复选框的值
    REST构架风格介绍:状态表述转移
    RESTful HTTP的实践infoQ
    C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
    Html TO Ubb and Ubb TO Html
    SQL 位运算
    Memcached真的过时了吗?【转帖】
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3737759.html
Copyright © 2011-2022 走看看