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. 初探JAVA中I/O流(二)

    1.缓冲输入文件 FileReader BufferedReader FileReader可以直接对文件进行读操作.但是简化编程,加快读取速度,我们加入了缓冲机制,使用了BufferedReader. ...

  2. [AaronYang]C#人爱学不学[2]

    1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...

  3. 第九章:Java----泛型学习(最后过一遍)

    泛型:让集合记住里面元素的类型,避免取出时需要强制类型转换(大到小).   ClassCastException! 编译阶段就能发现错误.  语法更严格! 更不容易犯错! 1. 构造器的名字还是类名, ...

  4. struts2支持的结果类型

    在struts2-core.jar/struts-default.xml中,我们可以找到关于result-type的一些配置信息,从中可以看出struts2组件默认为我们提供了这 些result-ty ...

  5. HDU-1698 JUST A HOOK 线段树

    最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  6. UOJ35 后缀数组(模板)

    #35. 后缀排序 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在原串中的位置.位置编号为 1 ...

  7. Uva11729 Commando War

    相邻两个士兵交换顺序,不会对其他的有所影响,贪心考虑两两之间交换策略即可. sort大法好.印象中这类排序题里有一种会卡sort,只能冒泡排序,然而到现在还没有遇到 /**/ #include< ...

  8. android anr分析方法

    目录(?)[+] 案例1关键词ContentResolver in AsyncTask onPostExecute high iowait 案例2关键词在UI线程进行网络数据的读写   一:什么是AN ...

  9. cf340 C. Watering Flowers

    C. Watering Flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. 初试visual studio2012的新型数据库LocalDB

    http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...