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 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
随机推荐
- java格式处理工具类
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- css考核点整理(三)-css选择器的使用
css选择器的使用
- 一次优化web项目的经历记录(一)
一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程.开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来, ...
- HTML特效代码大全
1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a>1)贴 ...
- Java中View游戏开发框架
java中游戏开发引擎View比较适合被动触发的游戏,不能使用于那种对战的游戏 Game01Activity.java 这里是调用的activity package cn.sun.syspro; i ...
- 前端过滤XSS攻击
日常开发过程中,对于存在用户交互的一些门户网站等,过滤xss攻击是必不可少的. 此处主要记录下我在工作过程中的简单处理方法. 前端过滤XSS攻击, 我这里用的是开源工程 js-xss,官网地址:htt ...
- Linq 调试
void Main() { var MyMonitor = new Devart.Data.Oracle.OracleMonitor(); MyMonitor.IsActive = true; var ...
- 表单提交对chrome记住密码的影响
在处理注册.登录等含有用户名,密码的元素的表单时,chrome会主动的提示记住密码,然而这个功能在用户名的选择上真是耐人寻味,它总是寻找离password input控件最近的那一个文本框的内容,作为 ...
- iOS开发之指定UIView的某几个角(小于4)为圆角
在iOS开发中,我们经常会遇到View设置圆角的问题,如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore ...
- iOS UICollectionview的详细介绍
转载自:http://jinqianchina.github.io/2015/08/16/UICollectionview%E7%9A%84%E4%BD%BF%E7%94%A8%E8%AF%A6%E8 ...