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)或 ...
随机推荐
- JDK13,不如温习下Java8
JDK13于9月17号正式GA,版本新特性可参考: https://www.oschina.net/news/109934/jdk-13-released 虽然JDK更新迅速,但开发者貌似并不买账,据 ...
- The 10 Most Important Linux Commands/10个最经常使用的命令行
1. ls 命令:to show all of the major directiories filed under a given file system. for example: ls /app ...
- Java程序连接数据库
/** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ // 第一种实现 /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ @Test p ...
- linux 堆栈查看
top -c 查看进程ID pstree PID 查看线程树 pstack PID 查看堆栈
- [VB.NET Tips]Select Case语句拾遗
正常的Select 语句如下: Dim status As Integer = 5 Select Case status Case 0 Console.WriteLine("状态是:0&qu ...
- 主动降噪技术(ANC)的前生今世--概念历史
一 概念 假如使用一句通俗的语言来概述ANC的原理的话,那就是:通过发出与噪声相位相反,频率.振幅相同的声波与噪声干涉实现相位抵消. 使用比较正式的语言来解释就是:动降噪通过降噪系统产生与外界噪音相等 ...
- Vue学习之不同组件之间的消息传递
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WebStorm2017.3.4版本 注册码
http://idea.singee77.com http://im.js.cn:8888
- java数据结构——二叉树(BinaryTree)
前面我们已经学习了一些线性结构的数据结构和算法,接下来我们开始学习非线性结构的内容. 二叉树 前面显示增.删.查.遍历方法,完整代码在最后面. /** * 为什么我们要学习树结构. * 1.有序数组插 ...
- Java 世界的盘古和女娲 —— Zygote
本文基于 Android 9.0 , 代码仓库地址 : android_9.0.0_r45 文中源码链接: Zygote.java ZygoteInit.java ZygoteServer.java ...