构造 + 离散数学、重言式 - POJ 3295 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<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的更多相关文章
- poj 3295 Tautology (构造)
题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
随机推荐
- HTML 统一资源定位器
URL 也被称为网址. URL 可以由单词组成,比如 “w3school.com.cn”,或者是因特网协议(IP)地址:192.168.1.253.大多数人在网上冲浪时,会键入网址的域名,因为名称比数 ...
- Gin-Go学习笔记六:Gin-Web框架 Api的编写
Api编写 1> Gin框架的Api返回的数据格式有json,xml,yaml这三种格式.其中yaml这种格式是一种特殊的数据格式.(本人暂时没有实现获取节点值得操作) 2> ...
- HTML网页实现flv视频播放
一.HTML代码如下: <div id="player"></div> 二.JavaScript代码如下: <script src="htt ...
- QML MouseArea学习小结
QML中的MouseArea类型为用户进行简单的鼠标操作提供了方便. MouseArea是一个不可见的Item,通常与可见项目结合使用,以便为该项目提供鼠标处理.通过有效地充当代理,鼠标处理的逻辑可以 ...
- xamarin Mqtt
1 什么是MQTT? mqtt (Message Queuing Telemetry Transport,消息队列遥测传输)是 IBM 开发的一个即时通讯协议,有可能成为物联网的重要组成部分.MQTT ...
- lower_case_table_names和数据库在Linux和windows平台之间的相互迁移问题
MySQL关于 lower_case_table_names 的文档 https://dev.mysql.com/doc/refman/5.7/en/identifier-case-sensitivi ...
- Docker搭建Nexus(Maven私库)
0.镜像的查找:docker search nexus 1.拉取官方镜像:docker pull sonatype/nexus3 2.创建了自己的目录 (/opt/nexus/nexus-data) ...
- <h1>~<h6> 标题标签
<h1>~</h6>标题系列标签 解释:h1到h6 中h1标签最大,h6标签最小,逐一递增. 例如: <h1>标签</h1> <h2>标签& ...
- tf.gather_nd()
tf.gather_nd( params, indices, name=None, batch_dims=0) TensorFlow链接:https://tensorflow.google.cn/ap ...
- Jmter(一)_时间戳
显示当前时间的使用Jmeter-Tools-Function Helper Dialog的__time 显示当前时间移动的使用__timeShift 有日期移动(e.g. P2D);时(PT2H);分 ...