[ACM] POJ 3295 Tautology (构造)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9302 | Accepted: 3549 |
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
Source
field=source&key=Waterloo+Local+Contest" style="text-decoration:none">Waterloo Local Contest
, 2006.9.30解题思路:
题意为依据输入的不同的字符串。来求这个逻辑表达式的值。
这里用到了栈,和表达式求值思想类似。从后往前扫面输入的字符串,假设是数(题中的p,q,r,s,t)就进栈。假设是操作符就从栈中取数。运算后再进栈。题中的数p,q,r,s,t仅仅有0,1取值,所以5重循环,对获得的表达式求值。
遇到0就退出。
代码:
#include <iostream>
#include <string.h>
#include <stack>
using namespace std; string wff;
int p,q,r,s,t;
bool ok; int compute(string str)//栈中的操作
{
stack<int>st;
int len=str.length();
for(int i=len-1;i>=0;i--)
{
if(str[i]=='p')
st.push(p);
else if(str[i]=='q')
st.push(q);
else if(str[i]=='r')
st.push(r);
else if(str[i]=='s')
st.push(s);
else if(str[i]=='t')
st.push(t);
else if(str[i]=='K')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x&y);
}
else if(str[i]=='A')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x||y);
}
else if(str[i]=='N')
{
int x=st.top();
st.pop();
st.push(!x);
}
else if(str[i]=='C')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(!x||y);
}
else if(str[i]=='E')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x==y);
}
}
return st.top();
} int main()
{
while(cin>>wff&&wff!="0")
{
ok=1;
for(p=0;p<2;p++)//枚举结果。遇到0就退出循环
for(q=0;q<2;q++)
for(r=0;r<2;r++)
for(s=0;s<2;s++)
for(t=0;t<2;t++)
{
if(compute(wff)==0)
{
ok=0;
goto label;
}
}
label:
if(ok)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}
[ACM] 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(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- 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 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
随机推荐
- NOIP真题汇总
想想在NOIP前总得做做真题吧,于是长达一个月的刷题开始了 涉及2008-2016年大部分题目 NOIP [2008] 4/4 1.传纸条:清真的三维DP 2.笨小猴:字符串模拟 3.火柴棒等式:打表 ...
- Android Market google play store帐号注册方法流程 及发布应用注意事项【转载】
[转载]http://www.cnblogs.com/zdz8207/archive/2012/07/09/google-play-store-registered.html Android Mark ...
- IIS Express配置多站点同时运行
环境:Win10 Pro.Visual Studio 2015 Community.IIS Express 10 VS2015集成IIS Express,所以无需单独下载, 默认安装位置:C:\Pro ...
- V-SQL的简单使用
V-SQL概述: V-SQL,是对同望V3平台时间多数据支持非常重要的基础引擎.因为各个数据库的查询语句的语法有所不同,V-SQL的功能是把查询语句解析为执行系统连接的数据库(MSSQL,Oracle ...
- fieldset ----- 不常用的HTML标签
fieldset 元素可将表单内的相关元素分组. <fieldset> 标签将表单内容的一部分打包,生成一组相关表单的字段. 当一组表单元素放到 <fieldset> 标签内时 ...
- [ Java ] [ UT ] [ Mock ] [ JUnit ] 單元測試的撰寫
最近新的專案用到很多的單元測試,對於單元測試有多了一歇的了解. 先寫下大綱,後面分篇寫出總結心得. 1. 單元測試要隔離對外部的關聯 2. Mock, spy 的用法時機差異 3. JUnit 4, ...
- JS——大小写转化
<script> var str = 'JavaScript'; console.log(str.toUpperCase());//小写转大写 console.log(str.toLowe ...
- Windows Server 2008无法远程连接
Server 2008 R2依次配置好之后,重启发现总是远程桌面时而连接不上.具体现象如下: 偶尔可以通过桌面远程连接连接到Server.以为是防火墙的问题,各种设置——甚至关闭,依然无法连接.反复重 ...
- 用svg做流程管理
说起流程管理这个功能,如果没有个动态图配合显示,简直就是太没有客户体验感了.就比如说请假流程吧,流程走到哪一步了,流程走向过程中都那些人审批的,审批的结果等等,如果就来个列表,也不是说不行,就是觉得太 ...
- LINUX C: 获取本地指定网卡的IP地址
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> ...