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)或 ...
随机推荐
- java需要了解和学习的技能
一:系统架构师是一个最终确认和评估系统需求,给出开发规范,搭建系统实现的核心构架,并澄清技术细节.扫清主要难点的技术人员.主要着眼于系统的“技术实现”.因此他/她应该是特定的开发平台.语言.工具的大师 ...
- Day 26 网络基础 3
tcpdump抓包 tcpdump -i eth0 port 80 -nn -S -i 指定网卡 port 指定端口号 http 80:ftp 21 :ssh 22:telnet 23:smtp 25 ...
- 使用ajax提交的json数据,产生筹码问题
使用ajax提交的json数据,我们必须添加produces注解,如下所示.否则将会产生乱码 方法一:添加produces注解 @ApiOperation(value = "删除日志&quo ...
- java中的逃逸分析
逃逸分析 public static StringBuffer craeteStringBuffer(String s1, String s2) { StringBuffer sb = new Str ...
- PHP的跨域问题
服务端的代码 public function test(){ header("Access-Control-Allow-Origin: http://cnblogs.com"); ...
- 连接电脑时,无法启用USB调试
原因: 手机悬浮球 解决方案: 取消悬浮球,停止一切悬浮应用 (下面的废话可以不听) 预置条件: 手机已经打开开发者模式 开启USB调试模式 电脑能检测到手机 故事背景: 经常用手机连接电脑进行adb ...
- Python学习-is和==区别, encode和decode
一.is 和 == 介绍 1. is 比较的是两个对象的内存地址是否相同,它们是不是同一个对象. 2. == 比较的是两个对象的内容是否相同. 在使用is前,先介绍Python的一个内置函数id( ...
- 常用的HDFS操作
首先,把Hadoop命令加入到PATH环境变量中,直接通过start-dfs.sh开启Hadoop,也可以直接通过hdfs命令访问HDFS中的内容,方便平时的操作. 配置PATH环境变量 vim ~ ...
- 4.7 if else-if
c#中的if else-if if else-if 中最后的"else":如果用户输入的不等于上面的else if(xxx)表达式,则输出这行代码. **不参与运算的数值不用转换 ...
- 读《深入理解Elasticsearch》点滴-对象类型、嵌套文档、父子关系
一.对象类型 1.mapping定义文件 "title":{ "type":"text" }, "edition":{ ...