Tautology
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10453   Accepted: 3967

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

Source

 
 
 
 
 
 
 
 
 
代码
#include<stdio.h>
#include<string.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
char str[200];
int p,q,r,s,t;
 stack<int>Q;
void instack(char c){
       if(c=='p')
       Q.push(p);
       else if(c=='q')
       Q.push(q);
       else  if(c=='r')
       Q.push(r);
       else  if(c=='s')
       Q.push(s);
       else  if(c=='t')
       Q.push(t);
       else
       return;
}
void calculate(char c){
    int a,b,temp;
     if(c=='K'){
         a=Q.top();
         Q.pop();
         b=Q.top();
         Q.pop();
         temp=a&b;
         Q.push(temp);
     }
     else  if(c=='A'){
         a=Q.top();
         Q.pop();
         b=Q.top();
         Q.pop();
         temp=a||b;
         Q.push(temp);
     }
     else  if(c=='E'){
        a=Q.top();
         Q.pop();
         b=Q.top();
         Q.pop();
         temp=(a==b);
         Q.push(temp);
     }
     else  if(c=='C'){
      a=Q.top();
         Q.pop();
         b=Q.top();
         Q.pop();
         temp=(!a)||b;
         Q.push(temp);
     }
     else  if(c=='N'){
         a=Q.top();
         Q.pop();
         temp=!a;
         Q.push(temp);
     }
     else
     return ;
}
int  main(){
    while(scanf("%s",str)!=EOF){
        getchar();
       if(strcmp(str,"0")==0)
       break;
       int len=strlen(str);
       int flag;
       for( p=0;p<=1;p++){
           for( q=0;q<=1;q++){
               for(r=0;r<=1;r++){
                   for( s=0;s<=1;s++){
                       for( t=0;t<=1;t++){
                            flag=0;
                           for(int i=len-1;i>=0;i--){
                               if(str[i]=='p'||str[i]=='q'||str[i]=='r'||str[i]=='s'||str[i]=='t')
                                  instack(str[i]);
                                  else
                                  calculate(str[i]);
                           }
                           int ans=Q.top();
                           Q.pop();
                           if(ans==0){
                              flag=1;
                              break;
                           }

}
                       if(flag) break;
                   }
                    if(flag) break;
               }
                if(flag) break;
           }
            if(flag) break;
       }
       if(flag)
       printf("not\n");
       else
       printf("tautology\n");
       memset(str,0,sizeof(str));
    }
    return 0;
}

poj3295的更多相关文章

  1. [POJ3295]Tautology

    [POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...

  2. POJ-3295 Tautology---栈+表达式求值

    题目链接: https://vjudge.net/problem/POJ-3295 题目大意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式WFF      其中      ...

  3. POJ-3295 Tautology (构造)

    https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...

  4. poj3295 Tautology —— 构造法

    题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...

  5. poj3295解题报告(构造、算术表达式运算)

    POJ 3952,题目链接http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为 ...

  6. POJ3295——Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...

  7. POJ3295 Tautology(枚举)

    题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...

  8. poj3295 Tautology , 计算表达式的值

    给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...

  9. POJ3295 Tautology(栈+枚举)

    Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...

随机推荐

  1. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  2. Java-编写一个jdbc操作类

    1.通过读取文件配置 package 数据库操作类; /* * Db.java Created on 2007年8月20日, 上午 8:37 */ import java.io.*; import j ...

  3. 【CodeForces 625C】K-special Tables

    题意 把1到n*n填在n*n的格子里.要求每一行都是递增的,使第k列的和最大. 分析 第k列前的格子1 2 .. 按要求填到满格,然后第k列及后面的格子,都从左到右填递增1的数. 第k列的和再加起来, ...

  4. RHCS

    简介 Red Hat Cluster Suite :红帽子集群套件 高可用性.高可靠性.负载均衡.存储共享 高可用集群是 RHCS 的核心功能.当应用程序出现故障,或者系统硬件. 网络出现故障时,应用 ...

  5. File类的创建,删除文件

    File.Create(@"C:\Users\shuai\Desktop\new.txt"); Console.WriteLine("创建成功"); Conso ...

  6. html5新标签转

    HTML 5 是一个新的网络标准,目标在于取代现有的 HTML 4.01, XHTML 1.0 and DOM Level 2 HTML 标准.它希望能够减少浏览器对于需要插件的丰富性网络应用服务(p ...

  7. MongoDB的安装 转

    第1章 MongoDB的安装 (黎明你好原创作品,转载请注明) 1.1 MongoDB简介 MongoDB是一个基于分布式文件存储的数据库开源项目.由C++语言编写,旨在为WEB应用提供可护展的高性能 ...

  8. Eclipse启动时卡死解决方法

    Eclipse未正常关闭时,再次启动通常会卡死,解决方法为:到<workspace>\.metadata\.plugins\org.eclipse.core.resources目录,删除文 ...

  9. [转载]给Jquery动态添加的元素添加事件

    原文地址:给Jquery动态添加的元素添加事件作者:小飞侠 我想很多人都会向我一样曾经 被新元素的事件绑定困惑很久也就是在页面加载完成后给元素绑定了事件,但又新增加的元素上却没有绑定任何事件. js的 ...

  10. .NET平台上的Memcached客户端介绍

    早上接到一个任务,需要对Linux服务器的Memcached的update操作进行性能测试,我发现我是一个典型的“手里拿着锤子, 就把所有问题都当成钉子”的人.我第一个念头就是,上Memcached的 ...