poj3295
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10453 | Accepted: 3967 |
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
Source
#include<string.h>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
char str[200];
int p,q,r,s,t;
stack<int>Q;
void instack(char c){
if(c=='p')
Q.push(p);
else if(c=='q')
Q.push(q);
else if(c=='r')
Q.push(r);
else if(c=='s')
Q.push(s);
else if(c=='t')
Q.push(t);
else
return;
}
void calculate(char c){
int a,b,temp;
if(c=='K'){
a=Q.top();
Q.pop();
b=Q.top();
Q.pop();
temp=a&b;
Q.push(temp);
}
else if(c=='A'){
a=Q.top();
Q.pop();
b=Q.top();
Q.pop();
temp=a||b;
Q.push(temp);
}
else if(c=='E'){
a=Q.top();
Q.pop();
b=Q.top();
Q.pop();
temp=(a==b);
Q.push(temp);
}
else if(c=='C'){
a=Q.top();
Q.pop();
b=Q.top();
Q.pop();
temp=(!a)||b;
Q.push(temp);
}
else if(c=='N'){
a=Q.top();
Q.pop();
temp=!a;
Q.push(temp);
}
else
return ;
}
int main(){
while(scanf("%s",str)!=EOF){
getchar();
if(strcmp(str,"0")==0)
break;
int len=strlen(str);
int flag;
for( p=0;p<=1;p++){
for( q=0;q<=1;q++){
for(r=0;r<=1;r++){
for( s=0;s<=1;s++){
for( t=0;t<=1;t++){
flag=0;
for(int i=len-1;i>=0;i--){
if(str[i]=='p'||str[i]=='q'||str[i]=='r'||str[i]=='s'||str[i]=='t')
instack(str[i]);
else
calculate(str[i]);
}
int ans=Q.top();
Q.pop();
if(ans==0){
flag=1;
break;
}
}
if(flag) break;
}
if(flag) break;
}
if(flag) break;
}
if(flag) break;
}
if(flag)
printf("not\n");
else
printf("tautology\n");
memset(str,0,sizeof(str));
}
return 0;
}
poj3295的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- POJ-3295 Tautology---栈+表达式求值
题目链接: https://vjudge.net/problem/POJ-3295 题目大意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式WFF 其中 ...
- POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...
- poj3295 Tautology —— 构造法
题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...
- poj3295解题报告(构造、算术表达式运算)
POJ 3952,题目链接http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为 ...
- POJ3295——Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
随机推荐
- [USACO2002][poj1947]Rebuilding Roads(树形dp)
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...
- 第十六课:一些奇葩的元素节点object,video
object元素 object这个元素,现在前端很少用到,但是像flash,svg等奇葩元素,必须嵌套在object对象元素中.现代浏览器用video,canvas代替这些元素. 之前做过图表和地图的 ...
- Linux svn 回滚版本库
Linux代码 svn up Index/ 然后找出要撤销的确切版本: Linux代码 svn log --limit 10 Index/tpl/css/global.css 根据log怀疑是 ...
- SQL基础知识总结(一)
1.union 和union all 操作符 1)union内部的select语句必须拥有相同的列,列也必须有相似的数字类型.同时,每条select语句中列的顺序相同. union语法(结果集无重复) ...
- DOM0,DOM2,DOM3事件,事件基础知识入门
事件是javascript和HTML交互基础, 任何文档或者浏览器窗口发生的交互, 都要通过绑定事件进行交互; 事件有DOM0, DOM2和DOM3的区分(别问我怎么少了一个DOM1, 也没找到DOM ...
- poppin_xpower_ 常城
- 转: maven进阶:一个多模块项目
一个多模块项目通过一个父POM 引用一个或多个子模块来定义.父项目,通过以下配置,将子项目关联. <packaging>pom</packaging> <modules& ...
- python多态
多态是面向对象语言的一个基本特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式.在处理多态对象时,只需要关注它的接口即可,python中并不需要显示的编写(像Java一 ...
- ubuntu安装spark on yarn
安装spark 安装hadoop 安装ssh,调试免密钥登录 配置hadoop 配置yarn 测试
- pthread_create线程创建的过程剖析
http://blog.csdn.net/wangyin159/article/details/47082125 在Linux环境下,pthread库提供的pthread_create()API函数, ...