构造 + 离散数学、重言式 - 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, ...
随机推荐
- HighChat 动态绑定数据记录
最近刚开始做图形操作,纠结了一上午,highchat 动态绑定数据这块一直不知道怎么绑定,后来多次尝试,发现 1.x轴的数据是个数组格式,我从后台传到前台的时候,js中用数组进行处理数据,然后赋值到c ...
- 解决IIS Web部署 svg/woff/woff2字体找不到问题(vue部署后找不到)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/smartsmile2012/articl ...
- 前端Q的小小小里程碑
很多关注我的人都不太了解前端Q这个名字的由来,这篇文章就来讲讲前端Q的前世今生,顺便送大大福利(文末有惊喜),哈哈-- 恭喜!前端Q总用户数突破千啦~~ 前端Q前世 前端Q这个公众号,其实很早很早的时 ...
- RMAN笔记
Rman常用命令 Preview选项 1) 显示用于还原system表空间数据文件的备份文件 RMAN> restore datafile 2 preview; 2) 显示用于还原特 ...
- Spring入门。
程序的耦合和解耦. 1.问题引入. 在使用jdbc和数据库交互时.注册驱动:DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());如 ...
- mysql--日志文件
1 选择常规查询日志和慢查询日志输出目标 1.1 log_output查看.定义 所谓的输出目标就是日志写入到哪里,mysql中用系统变量 log_output来指定输出目标,log_output的 ...
- m0n0防火墙安装配置方法
m0n0防火墙安装配置方法 准备工具: vmware虚拟机 m0n0防火墙安装镜像:M0n0Wall - generic-pc-1.8.1.iso 桥接网卡ip:192.168.43.0/24 hos ...
- 腾讯面试Android高级岗,居然被一个多线程基础面倒了?
前言 一个在深圳从事开发五年的老友一个月前从原公司辞职后,昨天去腾讯总部面试Android高级岗,一面的时候,自我介绍后,陆陆续续问了很多问题,有着五年的从业经验很多项目开发的技术问题都回答的很通顺, ...
- LOJ 2249: 洛谷 P2305: bzoj 3672: 「NOI2014」购票
题目传送门:LOJ #2249. 题意简述: 有一棵以 \(1\) 号节点为根节点的带边权的树. 除了 \(1\) 号节点的所有节点上都有人需要坐车到达 \(1\) 号节点. 除了 \(1\) 号节点 ...
- 关于ID命名 一个页面唯一
1.一般ID在一个区域内必须是唯一的.这样是一个规范而且在IE中使用JS通过ID获取这个对象永远只能获取第一个. 2.js无法找到重复的ID,用js获取时,只能得到第一个ID元素,但,如果不同的区域范 ...