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

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

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

题意:K  A  N  C  E 分别代表了不同的运算符,然后用栈模拟即可,,,构造法。。。。。。。。。。。。

#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring> using namespace std; const int N=; char str[N];
int p,q,r,s,t;
stack<int> st; int isvariables(char ch){
switch(ch){
case 'p':st.push(p);return ;
case 'q':st.push(q);return ;
case 'r':st.push(r);return ;
case 's':st.push(s);return ;
case 't':st.push(t);return ;
}
return ;
} void operators(char op){
switch(op){
case 'K':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x && y);
}break;
case 'A':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x || y);
}break;
case 'N':{
int x=st.top(); st.pop();
st.push(!x);
}break;
case 'C':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push((!x) || y);
}break;
case 'E':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x==y);
}break;
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%s",str) && str[]!=''){
int len=strlen(str);
int flag=;
for(p=;p<= && flag;p++)
for(q=;q<= && flag;q++)
for(r=;r<= && flag;r++)
for(s=;s<= && flag;s++)
for(t=;t<= && flag;t++){
for(int i=len-;i>=;i--)
if(!isvariables(str[i]))
operators(str[i]);
int last=st.top();
st.pop();
if(last==)
flag=;
}
if(flag)
printf("tautology\n");
else
printf("not\n");
}
return ;
}

POJ 3295 Tautology (构造法)的更多相关文章

  1. POJ 3295 Tautology(构造法)

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

  2. poj 3295 Tautology (构造)

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

  3. POJ 3295 Tautology 构造 难度:1

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

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

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

  5. POJ 3295 Tautology(构造法)

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

  6. 构造 + 离散数学、重言式 - POJ 3295 Tautology

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

  7. POJ 3295 Tautology (构造题)

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

  8. poj 3295 Tautology(栈)

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

  9. 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)或 ...

随机推荐

  1. OTL翻译(1) -- 说明

    说明 该文档说明的是4.0版本的ORACLE/ODBC和DB2-CLI模板库(OTL).OTL4.0(后面简称OTL)模板库是基于C++的模板的. OTL4.0是组合了C++的模板框架和OTL适配器. ...

  2. iOS开发-Quartz2D初识

    Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...

  3. C#中Serializable序列化

    序列化就是是将对象转换为容易传输的格式的过程,一般情况下转化打流文件,放入内存或者IO文件 中.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象,或 ...

  4. WPF 控件回车移动焦点

    .Set the TabIndex=" . private void detailGrid_Keydown(object sender, KeyEventArgs e) { try { if ...

  5. php -- 取路径:getcwd()、__DIR__、__FILE__ 的区别

    getcwd() :显示是 在哪个文件里调用此文件 的目录 __DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名

  6. C语言学习笔记:14_内部函数和外部函数

    /* * 14_内部函数和外部函数.c * * Created on: 2015年7月5日 * Author: zhong */ #include <stdio.h> #include & ...

  7. 【nodejs】使用put方式向后端提交数据

    页面代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...

  8. 一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码

    遍历表格的JS容易忘,留个随笔以备忘. var tableData="";    var table=document.getElementById("XXTableId ...

  9. mysql存储引擎简析

    一.常见存储引擎特性 Innodb 具有提交.回滚和崩溃恢复能力的事务安全.支持外键.使用mvcc以及行锁来提供事务支持,因此支持高并发.适用于写频繁,并发率高的应用. Myisam 不支持事务和灾难 ...

  10. hibernate:MySQL No Dialect mapping for JDBC type: -1

    出处:(hibernate中使用原生的sql语句,报如下错误:) MySQL No Dialect mapping for JDBC type: -1 代码: List list = session. ...