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. uni-app采坑记录

    1. uni-app采坑记录 1.1. 前言 这里记录下uni-app实践中踩的坑 1.2. 坑点 1.2.1. 触发事件@longTap和@longpress 这两个都表示长按触发事件,那么这两个有 ...

  2. disable_function绕过--利用LD_PRELOAD

    0x00 前言 有时候直接执行命令的函数被ban了,那么有几种思路可以bypass 1.使用php本身自带的能够调用外部程序的函数 2.使用第三方插件入(如imagick) 但是这两种无非就是利用ph ...

  3. Github标星过万,Python新手100天学习计划。

    大数据文摘编辑部出品 作为目前最火也是最实用的编程语言,Python不仅是新手入门程序界的首选,也逐渐成为了从大厂到小厂,招牌需求list的必要一条. 当然,学Python这件事情,你可能也和文摘菌一 ...

  4. 性能测试-MySQL性能查看(转)

    mysql查看数据库性能常用命令 mysql> show global status; 可以列出MySQL服务器运行各种状态值,另外,查询MySQL服务器配置信息语句: mysql> sh ...

  5. Shell 编程 排序工具 sort 和 uniq

    本篇主要写一些shell脚本排序工具的使用. sort 概述 sort是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序. 用法 sort [选项] 参数 -f:忽略大小写 -b ...

  6. Xpath re bs4 等爬虫解析器的性能比较

    xpath re bs4 等爬虫解析器的性能比较 本文原始地址:https://sitoi.cn/posts/23470.html 思路 测试网站地址:http://baijiahao.baidu.c ...

  7. matlab-画地形图

    1.画三维图 之前画曲面的三维图,运用z=x2+y2 算出z和Z,如果是给出数据的地形则没办法用公式算,为此,引入插值自动造出地形的坐标. 拟合和插值的区别:插值是必须要过点,曲线可以不光滑:拟合则是 ...

  8. Github api【Restful接口规范】

    Overview This describes the resources that make up the official GitHub REST API v3. If you have any ...

  9. 如何确定假设检验的样本量(sample size)?

    在<如何计算假设检验的功效(power)和效应量(effect size)?>一文中,我们讲述了如何根据显著性水平α,效应量和样本容量n,计算功效,以及如何根据显著性水平α,功效和样本容量 ...

  10. sparksql基础知识二

    目标 掌握sparksql操作jdbc数据源 掌握sparksql保存数据操作 掌握sparksql整合hive 要点 1. jdbc数据源 spark sql可以通过 JDBC 从关系型数据库中读取 ...