• POJ3295 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

    Source

     题意:给你一个表达式,表达式中有p,q,r,s,t五个变量,以及K,A,N,C,E五个函数

    求该表达式是否为永真式,即pqrst无论如何变化达标的的值始终是真的?

     题解:既然有表达式嘛,那么肯定是要栈来做表达式求值,这题无非多了几个运算符和几次枚举而已,然而因为看错题意WA了好几次,唉……

    代码如下:

    #include<map>
    #include<stack>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define fa puts("fuck");
    using namespace std;
    
    char d[505];
    int vis[2]= {0,0};
    
    int K(int a,int b)
    {
        return a&b;
    }
    
    int A(int a,int b)
    {
        return a|b;
    }
    
    int N(int a)
    {
        return !a;
    }
    
    int C(int a,int b)
    {
        return (!a)|b;
    }
    
    int E(int a,int b)
    {
        return a==b;
    }
    
    map<char,int> m;
    
    int dfs(int p,int q,int r,int s,int t)
    {
        int cnt;
        stack<int> stack1;
        for(int i=strlen(d)-1; i>=0; i--)
        {
            if(d[i]=='K'||d[i]=='A'||d[i]=='C'||d[i]=='E')
            {
                int tmp1=stack1.top();
                stack1.pop();
                int tmp2=stack1.top();
                stack1.pop();
                switch (d[i])
                {
                    case 'K':
                        stack1.push(K(tmp1,tmp2));
                        break;
                    case 'A':
                        stack1.push(A(tmp1,tmp2));
                        break;
                    case 'C':
                        stack1.push(C(tmp1,tmp2));
                        break;
                    case 'E':
                        stack1.push(E(tmp1,tmp2));
                }
            }
            else
            {
                if(d[i]=='N')
                {
                    int tmp1=stack1.top();
                    stack1.pop();
                    stack1.push(!tmp1);
                }
                else
                {
                    stack1.push(m[d[i]]);
                }
            }
        }
        int ans=stack1.top();
        while(!stack1.empty())
        {
            stack1.pop();
        }
        return ans;
    }
    
    int main()
    {
        while(scanf("%s",d),d[0]!='0')
        {
            memset(vis,0,sizeof(vis));
            for(int p=0; p<=1; p++)
            {
                m['p']=p;
                for(int q=0; q<=1; q++)
                {
                    m['q']=q;
                    for(int r=0; r<=1; r++)
                    {
                        m['r']=r;
                        for(int s=0; s<=1; s++)
                        {
                            m['s']=s;
                            for(int t=0; t<=1; t++)
                            {
                                m['t']=t;
                                vis[dfs(p,q,r,s,t)]=1;
                            }
                        }
                    }
                }
            }
            if(vis[0])
            {
                puts("not");
            }
            else
            {
                puts("tautology");
            }
        }
    
    }
    
    //k(a,b)=a&b
    //a(a,b)=a|b
    //e(a)=!a
    //c(a,b)=(!a)|b
    //e(a,b)=!(a^b)
  • 相关阅读:
    shutil模块详解
    pycharm连接服务器
    python中__name__属性的使用
    ORM学习笔记
    ORM连表操作
    python操作mysql实例
    python登录项目
    pycharm建立第一个django工程-----windows中
    打印顺序
    shell脚本
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/8433211.html
Copyright © 2020-2023  润新知