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 ...
随机推荐
- 机器学习中的矩阵方法(附录A): 病态矩阵与条件数
1. 病态系统 现在有线性系统: Ax = b, 解方程 很容易得到解为: x1 = -100, x2 = -200. 如果在样本采集时存在一个微小的误差,比如,将 A 矩阵的系数 400 改变成 4 ...
- 传说中的WeixinJSBridge和微信rest接口
直接上图,金山的APP“微信导航”,从界面上看有粉丝数等关键数据,实现了直接关注功能,莫不是rest接口?这江湖是大佬们的江湖,小喽啰只有眼馋的份咯. 很早就听说过WeixinJSBridge,不过官 ...
- javaSE文件的使用
1. package com.io.File; import java.io.*; import java.util.Date; public class UseFile { /** * 一些File ...
- ansible-1 的安装
该文章摘自:http://my.oschina.net/firxiao/blog/343395,该文章制作笔记使用,不做他用,转载请注明原文链接出处 Ansible 默认是基于SSH协议进行通信的. ...
- 【Gym 100685J】Just Another Disney Problem(交互/排序)
第一次做交互题. 题意是有n个数(n<1000),你通过问1 a b,后台返回你YES代表a<b,NO代表a>b.要你在10000次询问内给出一个符合的排列.n=1000来说,100 ...
- bzoj 1491 floyd
我们用w[i][j]表示i到j的最短路的数量,dis[i][j]表示i到j的最短路,那么我们在floyd的时候,如果dis[i][k]+dis[k][j]==dis[i][j],根据乘法原理我们就w[ ...
- Codevs1299 切水果
题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...
- Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State
目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ...
- [IOS UIScrollView+PageControl]信息展示横幅
ScrollViewController.h #import <UIKit/UIKit.h> @interface ScrollViewController : UIViewControl ...
- Nagios配置和命令介绍(二 )
Nagios配置 Nagios 主要用于监控一台或者多台本地主机及远程的各种信息,包括本机资源及对外的服务等.默认的Nagios 配置没有任何监控内容,仅是一些模板文件.若要让Nagios 提供服务, ...