POJ3295--Tautology(模拟)
来源:互联网 发布:php菜鸟教程 编辑:程序博客网 时间:2024/06/03 18:34
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.
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
题意
有K , C , A, E, N 五种运算符, p,q,r,s,t五种运算数,运算得到重言式输出tautology, 不是重言式输出not,
重言式是指永真式,即无论输入什么,结果都为1.
思路
运算数p , q , r, s, t都只有0,1两种输入,枚举所有情况即可。
map映射
只有32种情况,int型变量的二进制数就可以代表所以情况(状态压缩)
K 相当于 p && q
A 相当于 p || q
N 相当于 !p
C 相当于 p→q
E 相当于 p↔q
代码
#include <cstdio>#include <iostream>#include <stack>#include <map>#include <cstring>using namespace std;char str[110];stack<int > s;map<char, int> mp;int wwf;int len, n;int tmp1 , tmp2;void getbit(int i){ mp['s'] = (i >> 0) & 1; mp['p'] = (i >> 1) & 1; mp['q'] = (i >> 2) & 1; mp['r'] = (i >> 3) & 1; mp['t'] = (i >> 4) & 1;}bool gao(){ for(int i = 0; i < 32; i ++) { getbit(i); for(int j = len-1; j >= 0; j --) { switch(str[j]) { case 'K': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 && tmp2); //与 break; case 'A': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(tmp1 || tmp2);//或 break; case 'C': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push(!tmp1 || tmp2);//→ break; case 'N': tmp1 = s.top(); s.pop(); s.push(!tmp1);//非 break; case 'E': tmp1 = s.top(); s.pop(); tmp2 = s.top(); s.pop(); s.push((!tmp1 || tmp2)&&(tmp1 || !tmp2));//↔ break; default : s.push(mp[str[j]]); } } if(s.top() == 0) return false; } return true;}int main(){ while(cin >> str && str[0] != '0') { len = strlen(str); if(gao()) cout << "tautology" << endl; else cout << "not" << endl; }}
位运算大法好
- POJ3295 Tautology(模拟栈)
- POJ3295 Tautology (模拟)
- POJ3295--Tautology(模拟)
- POJ3295--Tautology
- poj3295-Tautology
- POJ3295 Tautology
- poj3295 Tautology
- poj3295--Tautology
- POJ3295-Tautology
- POJ3295-Tautology
- Poj3295 Tautology
- POJ3295 Tautology
- POJ3295-Tautology
- POJ3295 Tautology
- Tautology poj3295
- POJ3295 Tautology
- POJ3295 tautology 解题报告
- POJ3295 Tautology (遞歸+構造)
- python高级特性之生成器(generator)
- Java_基础—GUI(窗体/鼠标/键盘/动作监听和键盘事件)
- selenium webdriver 学习总结-Selenium API常用(四)
- 客户端JavaScript时间线
- sublime text 3 中的php代码语法检测
- POJ3295--Tautology(模拟)
- arguments 向array转化中 需要注意的一个小细节
- Spark的重要组件和基本工作流程
- 启发式算法(Heuristic Algorithm)
- ZooKeeper客户端 和服务器连接时版本问题
- 编程,更多?更好?更快?
- 浴谷夏令营题单
- selenium webdriver 学习总结-Selenium 控制测试流(五)
- 《MySQL入门很简单》学习笔记(9)之第9章触发器(关键词:数据库/MySQL/触发器)