构造 + 离散数学、重言式 - POJ 3295 Tautology
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 【题目来源】
Waterloo Local Contest, 2006.9.30
http://poj.org/problem?id=3295
【题目大意】
给你一个合式公式,让你判断是否为重言式。
【题目分析】
这题考了一些离散数学的概念在里面,题目并不难,就是构造加模拟。
使用一个栈来从后往前计算。 AC代码:
#include<cstring>
#define MAX 110
using namespace std;
int a[MAX];
char str[MAX];
void calc(int p,int q,int r,int s,int t)
{
int top=;
int t1,t2;
int len=strlen(str);
for(int i=len-;i>=;i--) //所有的都判断一遍,直到a[0]
{
if(str[i]=='p') a[top++]=p;
else if(str[i]=='q') a[top++]=q;
else if(str[i]=='r') a[top++]=r;
else if(str[i]=='s') a[top++]=s;
else if(str[i]=='t') a[top++]=t;
if(str[i]=='K')
{
t1=a[--top];
t2=a[--top];
a[top++]=t1&&t2;
}
else if(str[i]=='A')
{
t1=a[--top];
t2=a[--top];
a[top++]=t1||t2;
}
else if(str[i]=='N')
{
t1=a[--top];
a[top++]=!t1;
}
else if(str[i]=='C')
{
t1=a[--top];
t2=a[--top];
if(t1==&&t2==)
a[top++]=;
else a[top++]=;
}
else if(str[i]=='E')
{
t1=a[--top];
t2=a[--top];
if(t1==t2)
a[top++]=;
else a[top++]=;
}
}
}
bool judge()
{
int p,q,r,s,t;
for(p=;p<;p++) //枚举所有的情况
for(q=;q<;q++)
for(r=;r<;r++)
for(s=;s<;s++)
for(t=;t<;t++)
{
calc(p,q,r,s,t);
if(a[]==)
{
return false;
}
}
return true;
}
int main()
{
while(cin>>str)
{
if(strcmp(str,"0")==) break;
if(judge())
cout<<"tautology"<<endl;
else cout<<"not"<<endl;
}
}
构造 + 离散数学、重言式 - 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 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- 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 构造 难度: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(栈)
题目链接: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, ...
随机推荐
- 6 Linux用户和用户组管理
Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户都必须首相像系统管理员申请账号,然后以这个账号身份进入系统 每个用户账号都拥有一个唯一的用户名和各自的口令 用户在登陆时键入 ...
- flask 简单示例
flask import flask from flask import jsonify from flask import request # 要获取到请求参数的话,就要导入这个模块 ''' 创建接 ...
- Html快速上手
Html 概述 HTML文档 Doctype Meta Title Link Style Script 常用标签 各种符号 p 和 br a 标签 H 标签 select input:checkbox ...
- xadmin引入django-import-export导入功能
一.安装 由于xadmin自带的包里面已经包含了django-import-export 所以不用再pip install django-import-export了 但是xadmin管理后台只有导出 ...
- Android Studio总结
课程背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,G ...
- HTML基础四-VUE
一.VUE VUE官网:https://cn.vuejs.org/v2/guide/ 1.1 Vue简介 VUE与JS的区别 VUE在JS的基础上进行了优化 增加VUE最强大的特点,对数据操作特别的友 ...
- jdk,jre,jvm,openJdk
1,jdk install: java_home: point to the installation directory of JDK, which is used by some java dev ...
- selenium--设置浏览器的位置和高度宽度
前戏 web自动化的时候,如果你只有一个显示器,这时如果启动了谷歌浏览器,占据了整个显示器,那你肯定是没办法干其他的事情了.当然,你也可以使用phantomjs无头浏览器,那浏览器的兼容性你又不能不测 ...
- C# Cef winform 脚本的执行 踩过的坑
先从后端代码 如何执行js 代码谈起; 一个简单的需求: 在winform上点击一个按钮,实现登录信息的填入 流程: 就是让C#去运行一段脚本,. 运行的方式有两种, 异步与同步, 因为CEF是一帧一 ...
- cf1199解题报告
目录 cf1199解题报告 A B C D E F cf1199解题报告 发一波水题. A 模拟 #include <bits/stdc++.h> #define ll long long ...