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

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 【题目来源】
Waterloo Local Contest, 2006.9.30
http://poj.org/problem?id=3295
【题目大意】
给你一个合式公式,让你判断是否为重言式。
【题目分析】
这题考了一些离散数学的概念在里面,题目并不难,就是构造加模拟。
使用一个栈来从后往前计算。 AC代码:
#include<iostream>
#include<cstring>
#define MAX 110
using namespace std;
int a[MAX];
char str[MAX];
void calc(int p,int q,int r,int s,int t)
{
   int top=;
   int t1,t2;
   int len=strlen(str);
   for(int i=len-;i>=;i--) //所有的都判断一遍,直到a[0]
   {
       if(str[i]=='p') a[top++]=p;
  else if(str[i]=='q') a[top++]=q;
  else if(str[i]=='r') a[top++]=r;
  else if(str[i]=='s') a[top++]=s;
  else if(str[i]=='t') a[top++]=t;

if(str[i]=='K')
       {
          t1=a[--top];
          t2=a[--top];
          a[top++]=t1&&t2;
       }
       else if(str[i]=='A')
       {
          t1=a[--top];
          t2=a[--top];
          a[top++]=t1||t2;
       }
      else if(str[i]=='N')
       {
          t1=a[--top];
          a[top++]=!t1;
       }
      else if(str[i]=='C')
       {
          t1=a[--top];
          t2=a[--top];
          if(t1==&&t2==)
              a[top++]=;
          else a[top++]=;
       }
       else if(str[i]=='E')
       {
          t1=a[--top];
          t2=a[--top];
          if(t1==t2)
           a[top++]=;
          else a[top++]=;
       }
   }
}
bool judge()
{
   int p,q,r,s,t;
   for(p=;p<;p++)                       //枚举所有的情况
    for(q=;q<;q++)
     for(r=;r<;r++)
      for(s=;s<;s++)
       for(t=;t<;t++)
       {
           calc(p,q,r,s,t);
           if(a[]==)
           {
               return false;
           }
       }
    return  true;
}
int main()
{
   while(cin>>str)
   {
       if(strcmp(str,"0")==) break;
       if(judge())
           cout<<"tautology"<<endl;
       else cout<<"not"<<endl;
   }
}

构造 + 离散数学、重言式 - POJ 3295 Tautology的更多相关文章

  1. poj 3295 Tautology (构造)

    题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...

  2. POJ 3295 Tautology(构造法)

    http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...

  3. POJ 3295 Tautology(构造法)

    题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  4. POJ 3295 Tautology (构造题)

    字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...

  5. POJ 3295 Tautology (构造法)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7716   Accepted: 2935 Descrip ...

  6. POJ 3295 Tautology 构造 难度:1

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9580   Accepted: 3640 Descrip ...

  7. [ACM] POJ 3295 Tautology (构造)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 Descrip ...

  8. poj 3295 Tautology(栈)

    题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...

  9. poj 3295 Tautology 伪递归

    题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...

随机推荐

  1. 使用Git Bash向GitHub上传本地项目

    第一步:下载Git Bash(https://gitforwindows.org/),安装的过程是一路下一步,就不细说啦: 第二步:打开Git Bash,如下图显示: 第三步:现在让我们先放一放Git ...

  2. 某安全设备未授权访问+任意文件下载0day

    具体是哪家就不说了,硬件盒子,主要检测病毒. payload如下: https://xxx.xxx.xxx.xxx/downTxtFile.php?filename=/etc/passwd 比较简单, ...

  3. kubernetes学习之二进制部署1.16

    服务器规划和系统初始化 一.服务器规划 10.255.20.205 Master01 kube-apiserver.kube-controller-manager.kube-scheduler.ETC ...

  4. SQOOP的使用方法

    Sqoop是个命令行工具,用来在Hadoop和rdbms之间传输数据. 以Hadoop的角度看待数据流向,从rdbms往Hadoop是导入用sqoop import命令,反之从hadoop往rdbms ...

  5. HTTP Status 406 – Not Acceptable

    前端调用这个方法报错: HTTP Status 406 – Not Acceptable Type Status Report Description The target resource does ...

  6. 初学者git的用法

    初学者github的用法 1.在github上创建一个自己的工程 2.按着上面的要求执行你的命令行 3.将你的代码放到这个已经创建了.git的文件夹中,执行git add . 系统出现如下错误:war ...

  7. html--前端javascript初识

    一.JavaScript简介 JavaScript是一种基于对象和事件驱动并具有安全性能的脚本语言,有了JavaScript,可使网页变得生动.使用它的目的是与HTML超文本标识语言.Java 脚本语 ...

  8. Oracle EBS11i的下载

    1.登陆Oracle eDelivery https://edelivery.oracle.com/osdc/faces/Home.jspx 2.需要账号登录才能进行下载 chaomu@css.com ...

  9. three.js 离线API

  10. <虚树+树型DP> SDOI2011消耗战

    <虚树+树型DP> SDOI2011消耗战 #include <iostream> #include <cstdio> #include <cstring&g ...