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)或 ...
随机推荐
- [AWS] 01 - What is Amazon EMR
[DE] ML on Big data: MLlib 关于 Amazon EMR 发布版本 利用 Amazon EMR 分析大数据 Amazon Athena 是一种交互式查询服务,让您能够轻松使用标 ...
- commons-beanutils.jar及其支持文件
下载地址: 链接:https://pan.baidu.com/s/1AtiK3nsk0aBuBfMdNwBVGw 密码:6tul
- mysql 时间与字符串相互转换
时间.字符串.时间戳之间的相互转换:date转字符串.date转时间戳.字符串转date.字符串转时间戳.时间戳转date,时间戳转字符串用法 涉及的函数 date_format(date, form ...
- windows下查看进程及结束进程命令
windows下查看进程及结束进程命令 1)查看占用8080端口的进程号 >netstat –aon | findstr “8080” 结果:TCP 0.0.0.0:8080 ...
- python的__name__ == \'__main__\' 意义
转自http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- Spring Cloud 版本控制
### 正常版本 ``` org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE ``` ### SpringCloud 版 ...
- [Machine Learning] Linear regression
1. Variable definitions m : training examples' count \(y\) : \(X\) : design matrix. each row of \(X\ ...
- Spark 学习笔记之 union/intersection/subtract
union/intersection/subtract: import org.apache.spark.SparkContext import org.apache.spark.rdd.RDD im ...
- MongoDB 学习笔记之 地理空间索引入门
地理空间索引: 地理空间索引,可用于处理基于地理位置的查询. Point:用于指定所在的具体位置,我们以restaurants为例: db.restaurants.insert({name: &quo ...
- web前端之面试:自我介绍
面试官您好, 首先很感谢贵公司的面试邀请, 让我有这个幸运机会能来到这里和您交流 : 接下来我做一个简单的自我介绍: 我的姓名是 XX, 祖籍是XX, 年龄是24, 学校是 XXX, 专业是XXX: ...