题目来源:http://poj.org/problem?id=3295

Tautology

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 14016

Accepted: 5414

Description

WFF 'N PROOF is a logic game played with dice. Each die has sixfaces 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 thefollowing 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

tautology is a WFF that has value 1 (true)regardless of the values of its variables. For example, ApNp isa tautology because it is true regardless of the value of p. On theother 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 singleline containing a WFF with no more than 100 symbols. A line containing 0follows the last case.

Output

For each test case, output a line containing tautology or not asappropriate.

Sample Input

ApNp
ApNq
0

SampleOutput

tautology
not

Source

Waterloo Local Contest,2006.9.30

-----------------------------------------------------

解题思路

由于每个表达式最多只有5个变量:p,q,r,s,t. 所以每个表达式最多只有32种取值,遍历即可。

前缀表达式(波兰表达式)用递归计算,可参看NOI 2.2 递归 1696: 逆波兰表达式。

注意:&&和||有“短路”现象,即若左式=false, &&不会计算右式; 若左式=true, ||不会计算右式。

-----------------------------------------------------

代码

#include<fstream>
#include<iostream>
#include<string>
#include<set>
using namespace std;string s;
int mycount = 0;bool compute(const set<char> &pqrst, int j)          // 递归法解前缀表达式
{char ch = s.at(mycount++);int mycnt = 0 ,index = 0;set<char>::iterator it;bool left,right;switch(ch){case 'K': left = compute(pqrst, j); // &&,||有短路现象,即若左式=false,&&不会计右式;若左式=true,||不会计算右式right = compute(pqrst, j);return left&&right;break;case 'A': left = compute(pqrst, j);right = compute(pqrst, j);return left||right;break;case 'N': left = compute(pqrst, j);return !left;break;case 'C': left = compute(pqrst, j);right = compute(pqrst, j);return left<=right;break;case 'E': left = compute(pqrst, j);right = compute(pqrst, j);return left==right;break;default: for (it=pqrst.begin(); it!=pqrst.end(); it++) // 根据遍历计数器计算每个符号代表的值{if (ch==*it){index = mycnt;break;}mycnt++;}return (j>>index)%2;break;}
}int main()
{
#ifndef ONLINE_JUDGEifstream fin("3295.txt");int i = 0, cnt = 0, num = 0, j;char c;set<char> pqrst;bool is = true;while (fin >> s){if (s=="0"){break;}pqrst.clear();is = true;for (i=0; i<s.size(); i++){c = s.at(i);if (c=='p' || c=='q' || c=='r' || c=='s' || c=='t'){pqrst.insert(c);}}cnt = pqrst.size();num = 1<<cnt;for (j=0; j<num; j++){mycount = 0;is = compute(pqrst,j);if (!is){break;}}if (!is){cout << "not" << endl;}else{cout << "tautology" << endl;}}fin.close();
#endif
#ifdef ONLINE_JUDGEint i = 0, cnt = 0, num = 0, j;char c;set<char> pqrst;bool is = true;while (cin >> s){if (s=="0"){break;}pqrst.clear();is = true;for (i=0; i<s.size(); i++){c = s.at(i);if (c=='p' || c=='q' || c=='r' || c=='s' || c=='t'){pqrst.insert(c);}}cnt = pqrst.size();num = 1<<cnt;for (j=0; j<num; j++){mycount = 0;is = compute(pqrst,j);if (!is){break;}}if (!is){cout << "not" << endl;}else{cout << "tautology" << endl;}}
#endif
}

POJ 3295: Tautology相关推荐

  1. poj 3295 Tautology(经典构造算法题)

    思路:1)使用递归模拟,用备忘录优化,否则超时 另外:学到了一个不用递归即可枚举构造0-1序列的方法 for(i=0;i<32;i++)for(j=0;j<5;j++)arr[j]=(i& ...

  2. POJ 3295: Tautology 递归的永真式

    原题链接:Tautology 题目大意:p.q.r.s.t是逻辑变量,K.A.N.C.E是逻辑操作,相应的真值表已经给出.要求给定一个逻辑表达式,判断其是否为永真式,即无论其中的逻辑变量取值如何,其结 ...

  3. 【poj 3295】Tautology (栈)

    做法:穷举五个变量的可能取值.对于每种情况,建立一个栈,从右往左遍历字符串,字符代表数字时入栈,代表运算符时取出一定量的数字,运算后把结果压入栈.最后剩下的就是这个运算式的结果了. 在所有的取值下结果 ...

  4. php验证码百度ocr识别,利用百度OCR实现验证码自动识别

    在爬取网站的时候都遇到过验证码,那么我们有什么方法让程序自动的识别验证码呢?其实网上已有很多打码平台,但是这些都是需要money.但对于仅仅爬取点数据而接入打码平台实属浪费.所以百度免费ocr正好可以 ...

  5. poj日记(3295)

    转载:https://www.cnblogs.com/kuangbin/archive/2012/08/13/2636855.html Description WFF 'N PROOF is a lo ...

  6. POJ ZOJ题目分类

    POJ,ZOJ题目分类(多篇整合版,分类很细致,全面) 标签: 题目分类POJ整理 2015-04-18 14:44 1672人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: ACM资料(5) ...

  7. POJ,ZOJ题目分类(多篇整合版,分类很细致,全面)

    水题: 3299,2159,2739,1083,2262,1503,3006,2255,3094 初级: 一.基本算法:        (1)枚举 (1753,2965)       (2)贪心(13 ...

  8. NOIP 好题推荐(DP+搜索+图论)POJ ZOJ

    NOIP好题推荐(DP+搜索+图论)POJ ZOJ 1370 Gossiping (数论->模线性方程有无解的判断)+(图论->DFS)  1090 Chain ->格雷码和二进制码 ...

  9. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  10. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

最新文章

  1. Jmeter脚本 GUI和非GUI启动方式
  2. chrome 跨域插件
  3. 转《浅谈数据库设计技巧》
  4. Django 3.2.5博客开发教程:使用富文本编辑器添加数据
  5. Keep-Alive模式
  6. 20165232 缓冲区溢出漏洞实验
  7. it devops_DevOps是IT商店的战场
  8. SQL Server创建计划任务
  9. RabbitMQ 概念
  10. Mysql 忘记密码怎么办(win10)
  11. 解锁!95%的Android程序员做性能优化时,存在的五大误区和两大疑点!
  12. 蛋白二级结构预测(ANN神经网络、BP算法)
  13. 阿里品牌数据银行分析师复习资料库——基础标签
  14. Xilinx Zynq开发教程
  15. 【干货】2020年最新编程视频教程,速度收藏!!
  16. android手机定时截屏软件,手机截图软件哪个好 安卓手机长截图
  17. Oracle_本地计算机上的OracleOraDb11g_home1TNSListener 服务启动后停止
  18. Ubuntu登录后一直停留在桌面,只显示桌面背景
  19. 中国开源先驱的力与梦——开源六君子的黄金时代
  20. 市场调研报告-全球与中国云产品数据管理(PDM)软件市场现状及未来发展趋势

热门文章

  1. find命令的基础用法以及按文件修改时间查找文件
  2. 深度学习中神经网络的几种权重初始化方法
  3. OpenSSL-SNI
  4. 你真的打算凑合过完这一生吗(转)
  5. 目标检测---IoU计算公式
  6. 手把手教你搭建SpringCloud项目(十)集成OpenFeign服务接口调用
  7. 三分钟学会数据库, INSERT INTO 插入
  8. ZYNQ裸板:串口篇
  9. 2019 年第 30 周 DApp 影响力排行榜 | TokenInsight
  10. linux pvs命令安装,使用linux的pvs命令格式化输出物理卷信息报表