题目来源: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. MySQL删除表及删除表数据操作
  2. 安装GCC-8.3.0及其依赖
  3. ajax img标签,如何将属性添加到img标签以用于ajax调用
  4. MyBatis知多少(6)表现层与业务逻辑层
  5. 3 useReducer及其实现
  6. python调用c#注意事项_Python调用C#编写的DLL
  7. .NET CoreCLR开发人员指南(上)
  8. Java 8默认方法可能会破坏您的(用户)代码
  9. 图像影音型计算机主板选择什么,电脑主板型号在哪里看? 每日一答
  10. weblogic修改banner_WeblogicScanV1.3
  11. SQLite数据库---ListView控件之商品展示案例
  12. java structs hibernate php_eclipse+hibernate+structs 环境搭建
  13. 判断这5个数值是否连续相邻
  14. 【Android】Android--Dialog
  15. 思科模拟器Cisco Packet Tracer的下载与安装
  16. 对计算机系统进行软件攻击,win10系统预防电脑被系统漏洞攻击的操作方法
  17. 系统性能优化策略案例
  18. Unity 2017-2019各个版本unityhub下载连接
  19. 图片尺寸怎么修改?分享2种方法快速修改图片尺寸大小
  20. 梅特勒托利多xk3124电子秤说明书_梅特勒托利多称重仪表XK3124 B520

热门文章

  1. ODC V3.2.0 新版本发布 | 着重用户体验,挑战权限管控业务场景
  2. 部署 LAMP 平台
  3. 解决老Mac强行双系统后Mac系统引导丢失出现no bootable device
  4. Django框架学习——4—(DTL模板标签、模版常用过滤器、模版结构优化、加载静态文件)
  5. 项目管理知识体系指南(五)项目时间管理
  6. macbook卡在进度条开不了机_Mac 开机停在进度条解决方法
  7. 简单 黑苹果dsdt教程_提取DSDT和SSDT教程
  8. Docker网络之三:自定义容器虚拟IP
  9. 【华为OJ】【MML命令执行结果查询】
  10. 新猿木子李:0基础学python培训教程 Python操作Excel之读取数据