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

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

解题思路:

题意为依据输入的不同的字符串。来求这个逻辑表达式的值。

这里用到了栈,和表达式求值思想类似。从后往前扫面输入的字符串,假设是数(题中的p,q,r,s,t)就进栈。假设是操作符就从栈中取数。运算后再进栈。题中的数p,q,r,s,t仅仅有0,1取值,所以5重循环,对获得的表达式求值。

遇到0就退出。

代码:

#include <iostream>
#include <string.h>
#include <stack>
using namespace std; string wff;
int p,q,r,s,t;
bool ok; int compute(string str)//栈中的操作
{
stack<int>st;
int len=str.length();
for(int i=len-1;i>=0;i--)
{
if(str[i]=='p')
st.push(p);
else if(str[i]=='q')
st.push(q);
else if(str[i]=='r')
st.push(r);
else if(str[i]=='s')
st.push(s);
else if(str[i]=='t')
st.push(t);
else if(str[i]=='K')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x&y);
}
else if(str[i]=='A')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x||y);
}
else if(str[i]=='N')
{
int x=st.top();
st.pop();
st.push(!x);
}
else if(str[i]=='C')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(!x||y);
}
else if(str[i]=='E')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x==y);
}
}
return st.top();
} int main()
{
while(cin>>wff&&wff!="0")
{
ok=1;
for(p=0;p<2;p++)//枚举结果。遇到0就退出循环
for(q=0;q<2;q++)
for(r=0;r<2;r++)
for(s=0;s<2;s++)
for(t=0;t<2;t++)
{
if(compute(wff)==0)
{
ok=0;
goto label;
}
}
label:
if(ok)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}

[ACM] 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 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  3. POJ 3295 Tautology 构造 难度:1

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

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

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

  5. POJ 3295 Tautology(构造法)

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

  6. POJ 3295 Tautology (构造题)

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

  7. POJ 3295 Tautology (构造法)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7716   Accepted: 2935 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. hastable 用法

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似keyvalue的键值对,其中 ...

  2. vscode----vue中HTML代码tab键自动补全

    1.在vscode中插件下载并重新加载HTML Snippets 2.settings.json中配置files.associations对象. 找到setting.json文件:文件 --> ...

  3. Ubuntu下搭建repo服务器(二): 配置git-daemon-run

    git-daemon-run实际是一个脚本管理工具,用来启动git-daemon. 1 安装git-daemon-run(A端) apt-get install git-daemon-run 2. 配 ...

  4. MVC系列学习(九)-DTO的使用

    本次学习用的数据库,如下 1.什么是DTO:DataTransferObject 即数据传输对象,服务端的客户端的通信,自动定义个小的实体类,里面只包含我们需要传输的属性 2.不用DTO会有什么问题 ...

  5. 从"嘿,今晚..."谈消息安全传输中的技术点

    一.初级阶段:信息裸传 特点:在网络上传递明文 黑客定理一:网络上传递的数据是不安全的,属网络于黑客公共场所,能被截取 结果:传递明文无异于不穿衣服裸奔 改进方案:先加密,再在网络上传输 二.进阶阶段 ...

  6. css3 画小蜜蜂

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. GNSS数据下载网站

    Bernese 数据表文件下载 rinex文件下载 ftp://nfs.kasi.re.kr DCB.ION文件ftp://ftp.unibe.ch/AIUB/CODE/ 下载5.0更新文件 ftp: ...

  8. Java多线程学习笔记(一)——多线程实现和安全问题

    1. 线程.进程.多线程: 进程是正在执行的程序,线程是进程中的代码执行,多线程就是在一个进程中有多个线程同时执行不同的任务,就像QQ,既可以开视频,又可以同时打字聊天. 2.线程的特点: 1.运行任 ...

  9. CPU指令、机器码、程序和汇编语言

    一.指令 指令就是指挥机器工作的指示和命令.控制器靠指令指挥机器工作,人们用指令表达自己的意图,并交给控制器执行.一台计算机所能执行的各种不同指令的全体,叫做计算机的指令系统或指令集,每一台计算机均有 ...

  10. perf-perf stat用户层代码分析

    perf_event 源码分析 前言 简单来说,perf是一种性能监测工具,它首先对通用处理器提供的performance counter进行编程,设定计数器阈值和事件,然后性能计数器就会在设定事件发 ...