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. 【python+selenium学习】常见错误: 'gbk' codec can't decode byte 0xb0 in position 30

    最近编写的自动化脚本,数据部分使用到了从配置文件中取,即自定义config.ini,但是在读取配置文件的时候却报错了'gbk' codec can't decode byte 0xb0 in posi ...

  2. 浅谈华为验厂对MES系统的要求

    众所周知,华为对供应商在管理.防错.品控.追溯等方面的要求都非常严格.在华为验厂时,对供应商的信息系统,尤其是MES系统的评估也是有非常具体的要求.那么我们今天就来谈谈华为验厂时,对MES系统有哪些具 ...

  3. Spring Boot 2 整合 Dubbo 框架 ,实现 RPC 服务远程调用

    一.Dubbo框架简介 1.框架依赖   图例说明: 1)图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代 ...

  4. python中的__init__(self)是什么意思呢

    python中的__init__(self)是什么意思呢 init(self)这个时类的初始化函数 1 2 3 4 class Obj: def init(self): print 1 obj = O ...

  5. flask实战-个人博客-数据库-生成虚拟数据 --

    3.生成虚拟数据 为了方便编写程序前台和后台功能,我们在创建数据库模型后就编写生成虚拟数据的函数. 1)管理员 用于生成虚拟管理员信息的fake_admin()函数如下所示: personalBlog ...

  6. lynis安装和扫描Linux的安全漏洞

    今天客户的服务器出了点问题需要排查具体的原因,在德国朋友的建议下用Lynis进行扫描,Lynis是Linux系统中的审计工具,能够对Linux系统的安全进行检测,在对系统进行扫描检测后,会生成安全报告 ...

  7. 11-cmake语法-函数和宏的定义

    cmake语法不仅仅可以适用于 CMakeLists.txt,也适用于 xxx.cmake 文档. 在 OpenCV 的 CMakeLists.txt 中,很多 ocv_XXX() 的函数,都是定义在 ...

  8. Python 文件读写操作实例详解

    Python提供了必要的函数和方法进行默认情况下的文件基本操作.你可以用file对象做大部分的文件操作 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前 ...

  9. Jenkins集成allure测试报告

    前言 Allure框架是一个功能强大的自动化测试报告工具,不仅支持多种编程语言,而且能够完美的与各种集成工具结合,包括Jenkins,TeamCity,Bamboo,Maven等等,因此受到了很多测试 ...

  10. 【Linux】查看端口和进程

    netstat -tunlp ps -ef|grep process_name