POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295
题目:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13231 | Accepted: 5050 |
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 思路:
枚举 p, q, r, s, t的所有情况,共2^5种情况,对于每种情况都对给定的字符串进行操作,只要有一种情况是假的,结果就是假的,反之则是永真。
字符串长度不会超过100,所以最坏的情况是2^5*100 肯定不会超时。 对字符串的处理:从字符串尾部进行判断,如果是p,q,r,s,t就压入栈,如果是逻辑符号,就取栈顶元素运算。 代码:
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
char str[];
int p,q,r,s,t;
int solve(){
stack<int>num;
int len=strlen(str);
for (int i=len-; i>=; i--) {
if(str[i]>'a' && str[i]<'z'){
if(str[i]=='p') num.push(p);
if(str[i]=='q') num.push(q);
if(str[i]=='r') num.push(r);
if(str[i]=='s') num.push(s);
if(str[i]=='t') num.push(t);
}else{
int a=num.top();num.pop();
if(str[i]=='N') num.push(!a);
else{
int b=num.top();num.pop();
if(str[i]=='K') num.push(a&b);
if(str[i]=='A') num.push(a|b);
if(str[i]=='C') num.push((!a)|b);
if(str[i]=='E') num.push(a==b?:);
}
}
}
return num.top();
}
int main() {
while (gets(str)!=NULL && str[]!='') {
int flag=;
for (p=; p< ; p++) {
for (q=; q<; q++) {
for (r=; r<; r++) {
for (s=; s<; s++) {
for (t=; t<; t++) {
if(solve()==){
flag=;
break;
}
}
}
}
}
}
if(flag) printf("not\n");
else printf("tautology\n");
}
return ;
}
POJ 3295 Tautology(构造法)的更多相关文章
- 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 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- 构造 + 离散数学、重言式 - 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 枚举每个逻辑 ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- 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)或 ...
随机推荐
- Single Number 普通解及最小空间解(理解异或)
原题目 Given a non-empty array of integers, every element appears twice except for one. Find that singl ...
- PTA A1007&A1008
第四天 A1007 Maximum Subsequence Sum (25 分) 题目内容 Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 导出 mysql 数据到 redis
决定你要导入到 redis 的数据类型 假设我的表 t_user 的结构为 列名 注释 类型 name 名称 varchar idcard 身份证号 varchar phone 手机号 varchar ...
- PostMethod和GetMethod用法
注:新浪短接口参考地址:https://www.douban.com/note/249723561/ 将长的url链接转换成短链接 一.GetMethod try { HttpClient ...
- centos 下安装 Let’s Encrypt 永久免费 SSL 证书
功能 https证书,免费版,每三个月续签一次,可以用过脚本自动续签 安装 ssh登录到域名配置所在的主机(nginx,apache等) 安装git yum -y install git 输入 git ...
- jQuery鼠标滑过横向时间轴效果
jQuery鼠标滑过横向时间轴效果---效果图: jQuery鼠标滑过横向时间轴效果---全部代码: <!DOCTYPE html> <html> <head> & ...
- 什么是VR中的vection?
Vection是VR领域的一个专有名词,其义指“在虚拟现实中给人带来‘移动’(self-motion)感觉的认知因素”1.也就是说,vection就是指那些给玩家带来“我正在这个虚拟环境中移动”这种感 ...
- Windows10+YOLOv3实现检测自己的数据集(1)——制作自己的数据集
本文将从以下三个方面介绍如何制作自己的数据集 数据标注 数据扩增 将数据转化为COCO的json格式 参考资料 一.数据标注 在深度学习的目标检测任务中,首先要使用训练集进行模型训练.训练的数据集好坏 ...
- SpringBoot使用thymeleaf模板引擎引起的模板视图解析错误
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as ...
- Java-Thread01之创建线程
------  ...