Tautology
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 就是离散数学的一个模拟,判断所给的式子是不是永真公式
用栈逆序推还不算难
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
stack<int>sta;
int p,q,r,s,t,a,b;
char str[];
int f()
{
int len=strlen(str);
for(int i=len-; i>-; i--)
{
if(str[i]=='p')sta.push(p);//是字符直接入栈
else if(str[i]=='q')sta.push(q);
else if(str[i]=='r')sta.push(r);
else if(str[i]=='s')sta.push(s);
else if(str[i]=='t')sta.push(t);
else if(str[i]=='K')//是运算就进行运算
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
sta.push(a&&b);
}
else if(str[i]=='A')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
sta.push(a||b);
}
else if(str[i]=='C')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
if(a&&!b)sta.push();
else sta.push();
}
else if(str[i]=='E')
{
a=sta.top();
sta.pop();
b=sta.top();
sta.pop();
if(a==b)sta.push();
else sta.push();
}
else if(str[i]=='N')
{
a=sta.top();
sta.pop();
sta.push(!a);
}
}
return sta.top();
}
int sf()//枚举各种情况
{
for(p=; p<; p++)
for(q=; q<; q++)
for(r=; r<; r++)
for(s=; s<; s++)
for(t=; t<; t++)
if(!f())
return ;
return ;
}
int main()
{
while(gets(str))
{
if(!strcmp(str,""))
break;
else
{
if(sf())
printf("tautology\n");
else
printf("not\n");
}
}
return ;
}
Tautology的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- Tautology(structure)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10061 Accepted: 3826 Descri ...
- Tautology 分类: POJ 2015-06-28 18:40 10人阅读 评论(0) 收藏
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10428 Accepted: 3959 Descri ...
- poj 3295 Tautology
点击打开链接 Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8127 Accepted: 3115 ...
- POJ3295——Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- 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 (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
随机推荐
- POJ3723 Conscription
http://poj.org/problem?id=3723 这题虽然简单,但是还是错了很多次. 因为这题构建的图可能是不连通的.也就是说可能有很多棵树. 所以我以前写的并查集用在这上面会出问题的. ...
- DM8168 GPIO驱动与測试程序
本次測试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO 还是老规矩,贴上driver.c,Makefile,test.c: dm8168_gpio.c: #include &l ...
- change buffer
https://yq.aliyun.com/articles/222 change buffer: insert buffer delete buffer purge buffer 1.innodb_ ...
- cflow察看工程函数调用关系+Linux 0.11 内核实验环境
http://savannah.gnu.org/projects/cflow http://tinylab.org/linux-0.11-lab/ http://ftp.gnu.org/gnu/cfl ...
- SSL 错误
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.net.ssl.in ...
- linux device driver —— ioctl
实现了应用程序和设备驱动通过ioctl通信.还是对设备驱动没什么感觉,贴一下代码吧. 在Ubuntu 16.04 64bit中测试通过 ioctldemo.c #include <linux/m ...
- 省市联级菜单--js+html
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- linux与Windows共享文件配置
linux与Windows共享文件配置: 1.进入超级用户:$su root 2.启动tftp服务器:#netstat -a | grep tftp,出现如图所示的消息表示tftp服务器已安装成功: ...
- WPF 媒体播放器(MediaElement)实例,实现进度和音量控制
WPF 视频音频播放控件MediaElement实现进度控制,音量控制实例 说明: 1.Volume控制音量的大小,double类型,并且实现了属性依赖,可以用来双向绑定:在 0 和 1. 之间的线性 ...
- VS2015 Cordova Ionic移动开发(四)
一.布局 Ionic模板提供了一个侧边栏菜单示例项目和标签选项卡示例项目.本案例将两个布局进行结合,简单介绍下Ionic的布局.Ionic采用自定义标签和标准Html标签相结合.相对于全部使用div方 ...