构造 + 离散数学、重言式 - 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, ... 
随机推荐
- React路由安装使用和多种方式传参
			安装路由 npm i react-router-dom -S 引入路由 import { BowserRouter as Router, Route, Switch, ... } from " ... 
- 为什么MES实施起来效果不佳?
			原因一:我国制造业存在管理基础的先天不足 我国企业与发达国家企业在管理发展上,存在较大的差别.发达制造国家经历了管理探索.发展.成熟.再提高的全过程,从管理基础的奠定到思想认识的深刻程度,都是我国所无 ... 
- Es6学习指南-1-函数变量
			本篇章我们简述的是 es6初级知识点,认识es6,以及es6变量和es5的变量和函数. ECMAScript 6简介 ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代 ... 
- Java面经入口(持续更新...)
			大数据面经 https://zhuanlan.zhihu.com/go-for-it Java 基础知识复习 您可能不知道Java基础40道常见面试题及详细答案 hashcode详解 Java St ... 
- Nginx+Mysql调优
			使用nginx实现反向代理作用,具备负载均衡的功能. 接受客户端的请求 | nginx(宿主机) | |-------------------| web1 web2 (客户机) 原理: 与 ... 
- HLOJ1366 Candy Box 动态规划(0-1背包改)
			题目描述: 给出N个盒子(N<=100),每个盒子有一定数量的糖果(每个盒子的糖果数<=100),现在有q次查询,每次查询给出两个数k,m,问的是,如果从N个盒子中最多打开k个盒子(意思是 ... 
- UDP基础
			UDP主要特点: (1)UDP是无连接的,发送数据不需要建立连接,减少了开销和发送数据之前的时延. (2)UDP使用尽最大努力交付,即不保证可靠交付,因此主机不需要维持连接状态表. (3)UDP面向报 ... 
- 移动端好用的下拉加载上拉刷新插件  dropload插件
			入了很多下拉加载上拉刷新的插件,但是感觉都不好用,知道最近遇到这款dropload的插件,瞬间打开新世界的大门啊,无卡顿简单易用可配置 <!doctype html> <html&g ... 
- linux修改服务器时区并使用所选时区的时间
			linux 修改服务器时区并使用所选时区的时间(以 ubuntu18.04 修改为美国芝加哥时区为例) 一.修改时区 /usr/bin/tzselect 1.选择地区, 美洲地区: ... 
- centos7运维记录文档
			问题一:故障记录时间2019年4月4日,查看系统日志报错如下: tail -f /var/log/messages Apr 4 16:29:16 localhost kernel: tracker-e ... 
