poj 3295 Tautology
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8127 | Accepted: 3115 |
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
题目大意是给你一个逻辑判断语句,让你判断是否为用真句,其中小写字母代表逻辑变量,大写字母代表的是逻辑运算符,其中逻辑运算符的运算规则已经在表里没出了,如果不是用真就输出not,如果是用真就输出tautology
解题方法也没简单,建立一个堆栈,然后就类似表达式求值了,但是这里需要说明一下运算符的表示方法
K--------- &
A--------- |
N-------- !
C(这里让我头疼了好久,数字逻辑没学好啊,没看出来什么关系,看了别人的解析才发现的,比赛中遇到只能写一个函数根据表中的关系得到逻辑关系了)----(!x)| y
E-------- ==
自己写了一个简易的栈,所以代码有点长用STL会短一些,还有就是解题时特意用了一下用一个整形表示了5个数的状态,练习一下位运算了
AC代码 0MS
#include<stdio.h>
#include<string.h>
class bool_stack
{
public:
bool s[200];
int top;
bool_stack()
{
top = 0;
}
void push(bool a)
{
s[top++] = a;
}
bool pop()
{
top--;
return s[top]; }
};
int solve(bool * num, char * str)
{
bool_stack stack;
int len = strlen(str);
int i;
for(i = len - 1; i > -1; i--)
{
bool temp1, temp2;
switch(str[i])
{
case 'K':
temp1 = stack.pop();
temp2 = stack.pop();
stack.push(temp1 & temp2);
break;
case 'A':
temp1 = stack.pop();
temp2 = stack.pop();
stack.push(temp1 | temp2);
break;
case 'N':
temp1 = stack.pop();
stack.push(!temp1);
break;
case 'C':
temp1 = stack.pop();
temp2 = stack.pop();
stack.push((!temp2) | temp1);
break;
case 'E':
temp1 = stack.pop();
temp2 = stack.pop();
stack.push(temp1 == temp2);
break;
default:
stack.push(num[str[i] - 'p']);
break;
}
}
return stack.pop();
}
int main()
{
char str[120];
while(scanf("%s", str), str[0] != '0')
{
int flag = 0;
int i;
for(i = 0; i < 32; i++)
{
bool num[5];
int j;
for(j = 0; j < 5; j++)
{
num[j] = (i >> j) & 1;
}
if(solve(num, str) == 0)
break;
}
if(i == 32)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}
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 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
随机推荐
- Linux下升级python
本文的Linux系统为CentOS 7 64 在Linux系统的下载文件夹中邮件打开终端,输入命令: wget http://www.python.org/ftp/python/3.4.4/Pytho ...
- Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 (主要是NSLayoutConstraint 的使用)
当前位置: > Swift新手入门 > Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 时间:2014-09-10 16:49来源:未知 作者:啊成 举报 点击:5 ...
- git基本使用
说明:当前在自己的分支:self:远端开发分支:dev. 1. 基本命令: git status git add . git commit -m 'modify' //从远端dev拉取 git pul ...
- 【spring】non-compatible bean definition of same name and class
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected excep tion parsing XML do ...
- [转]使用eclipse+pydev远程调试OpenStack
作者:张华 发表于:2014-01-17版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 1, ...
- [转] matlab figure最大化
http://blog.163.com/yinhexiwen@126/blog/static/6404826620122942057214/ % figure 窗口最大化,坐标轴也随着窗口变大而相应变 ...
- linux杂谈
1. 目录的stick位 一般情况下,如果一个用户对一个目录有写权限,那么他就可以删除该目录下的文件,即使这些文件不是他的.为了防止这种情况,我们需要为目录设置stick位: chmod a+t yo ...
- Windows Server 2008 - How to Move the Quorum Disk Group
I received this question from a friend the other day - asking how on a Windows Server 2008 cluster y ...
- 就Double、Decimal来说数据计算异常
场景: 客户提示发料时提示库存不足,可肉眼比对发料数量与库存数量没有一点问题. 但调度跟踪却发现出现“不可思议”的异常. 简单分析: 1.转Decimal再计算没问题.精度较高,存储方法也不一样,所以 ...
- SIP 状态码
SIP应答消息状态码 与功能 类型 状态码 状态说明临时应答(1XX) 100 Trying 正在处理中180 Ringing 振铃181 call being forwarder 呼叫正在前向182 ...