POJ 3295:Tautology
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10482 | Accepted: 3982 |
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
这个题记得是离散数学里面的内容,题意是判断给定的字符串是不是永远为1,就是不管p、q、r、s、t取什么值,其结果都是1。
1AC。反正自从遇到了上一次类似的题目之后,做这种题自己的感受就是两点:
1.构造一个栈
2.从后往前面撸。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std; stack <int> o_sta;
int p,q,r,s,t;
string test;
int len,i; void push1(char a)
{
switch (a)
{
case 'p':
o_sta.push(p);
break;
case 'q':
o_sta.push(q);
break;
case 'r':
o_sta.push(r);
break;
case 's':
o_sta.push(s);
break;
case 't':
o_sta.push(t);
break;
default:
break;
}
} void cal(char a)
{
int temp1,temp2;
switch (a)
{
case 'N':
temp1=o_sta.top();
o_sta.pop();
temp1=!temp1;
o_sta.push(temp1);
break;
case 'K':
temp1=o_sta.top();
o_sta.pop();
temp2=o_sta.top();
o_sta.pop();
temp1=temp1&temp2;
o_sta.push(temp1);
break;
case 'A':
temp1=o_sta.top();
o_sta.pop();
temp2=o_sta.top();
o_sta.pop();
temp1=temp1|temp2;
o_sta.push(temp1);
break;
case 'C':
temp1=o_sta.top();
o_sta.pop();
temp2=o_sta.top();
o_sta.pop();
temp1=temp1-temp2;
if(temp1==1)
o_sta.push(0);
else
o_sta.push(1);
break;
case 'E':
temp1=o_sta.top();
o_sta.pop();
temp2=o_sta.top();
o_sta.pop();
temp1=temp1-temp2;
if(temp1==0)
o_sta.push(1);
else
o_sta.push(0);
break;
default:
break;
}
} bool solve()
{
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++)
{
for(i=len-1;i>=0;i--)
{
if(test[i]=='p'||test[i]=='q'||test[i]=='r'||test[i]=='s'||test[i]=='t')
push1(test[i]);
else
cal(test[i]);
}
if(o_sta.top()==0)
return false;
}
}
}
}
}
return true;
}
int main()
{
while(cin>>test)
{
if(test=="0")
break;
len=test.length(); if(solve())
{
cout<<"tautology"<<endl;
}
else
{
cout<<"not"<<endl;
}
}
//system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3295:Tautology的更多相关文章
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- 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
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 枚举每个逻辑 ...
随机推荐
- python笔记12
day12 今日内容 函数中高级(闭包/高阶函数) 内置函数 内置模块(.py文件) 内容回顾 函数基础概念 函数基本结构 def func(arg): return arg; v1 = func(1 ...
- 【高软作业4】:Tomcat 观察者模式解析 之 Lifecycle
一. 预备 如果你是Windows用户,使用Eclipse,并且想自行导入源码进行分析,你可能需要:Eclipse 导入 Tomcat 源码 如果你已遗忘 观察者模式,那么你可以通过该文章回顾:设计模 ...
- NO30 磁盘分区--Raid--ext2文件系统
Raid: ext2文件系统:
- 小程序转uni-app用到的一些方法
setData: function (obj) { let that = this; Object.keys(obj).forEach(function (key) { that.$set(that. ...
- Java笔记--多线程
1.线程的创建与运行(方式一): --1)创建一个Thread的子类: --2)重写Thread类的run()方法: --3)创建一个子类的对象: --4)调用线程的start()方法来启动线程,Ja ...
- Azure Cognitive Services- Speech To Text
Speech 服务是认知服务的一种,提供了语音转文本,文本转语音, 语音翻译等,今天我们实战的是语音转文本(Speech To Text). STT支持两种访问方式,1.是SDK,2.是REST AP ...
- Redis详解(八)——企业级解决方案
Redis详解(八)--企业级解决方案 缓存预热 缓存预热就是系统上线后,提前将相关的缓存数据直接加载到缓存系统.避免在用户请求的时候,先查询数据库,然后再将数据缓存的问题!用户直接查询事先被预热的缓 ...
- ej3-1优先使用静态工厂方法而非构造函数来创建对象
背景 很早之前就已经自己翻译了,先简单的贴出来,并做一下回顾. 条款1 优先使用静态工厂方法而非构造函数来创建对象 允许客户端创建一个实例的传统方法是:提供一个公共构造函数:有另外一个必须成为每个程序 ...
- 关于list
//问题一: List<string> list = new List<string>(); list = null; //这样写可以使用,但是当list值为null时会报未将 ...
- manjaro更换深度桌面 卸载ked桌面
下载需要的桌面环境 pacman -S deepin deepin-extra 修改/etc/lightdm/lightdm.conf sudo nano /etc/lightdm/lightdm.c ...