题目链接:

  http://poj.org/problem?id=3295

题目描述:

  给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0。K, A, N, C, E 分别表示且,或,非,真蕴含,等值。问表达式是不是永真的,如果是输出“tautology”,否则输出“not”。

解题思路:

  这里借用到了递归的本质,也就是对栈的模拟,用递归进行压栈,求表达式的值,再加上对变量状态压缩进行枚举。

 #include <cstdio>//本代码用G++交就ac,c++交就wa,因为G++是从前向后求值的,c++是从后向前求值的,
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 105
char str[maxn];
int index; bool dfs (int num);//对表达式求值
bool judge ();//对变量的取值进行枚举
int main ()
{
while (scanf ("%s", str), strcmp(str, ""))
{
if (judge ())
printf ("tautology\n");
else
printf ("not\n");
}
return ;
}
bool dfs (int num)
{
index ++;
if (str[index] == 'p')
return num & ;
else if (str[index] == 'q')
return (num >> ) & ;
else if (str[index] == 'r')
return (num >> ) & ;
else if (str[index] == 's')
return (num >> ) & ;
else if (str[index] == 't')
return (num >> ) & ;
else if (str[index] == 'K')
return dfs (num) & dfs (num);//这里的& | 不能用&& ||,因为后者使用时前面的为假就会停止运算,不能达到预计的效果
else if (str[index] == 'A')
return dfs (num) | dfs (num);
else if (str[index] == 'N')
return !dfs (num);
else if (str[index] == 'C')
return !dfs(num) | dfs(num);
else if (str[index] == 'E')
return dfs (num) == dfs(num); }
bool judge ()
{
int i;
for (i=; i<; i++)
{
index = -;
if (!dfs (i))
return ;
}
return ;
}

poj 3295 Tautology 伪递归的更多相关文章

  1. poj 3295 Tautology (构造)

    题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...

  2. poj 3295 Tautology(栈)

    题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...

  3. POJ 3295 Tautology(构造法)

    http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...

  4. POJ 3295 Tautology(构造法)

    题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  5. 构造 + 离散数学、重言式 - POJ 3295 Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...

  6. POJ 3295 Tautology (构造题)

    字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...

  7. poj 3295 Tautology

    点击打开链接 Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8127   Accepted: 3115 ...

  8. POJ 3295 Tautology 构造 难度:1

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9580   Accepted: 3640 Descrip ...

  9. POJ 3295 Tautology (构造法)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7716   Accepted: 2935 Descrip ...

随机推荐

  1. 对CSS尺寸单位'em'的长期误解

    一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...

  2. the attribute buffer size is too small 解决方法

    在进行查询的时候引发The attribute buffer size is too small错误解决 http://bbs.esrichina-bj.cn/esri/viewthread.php? ...

  3. 【APUE】信号量、互斥体和自旋锁

    http://www.cnblogs.com/biyeymyhjob/archive/2012/07/21/2602015.html http://blog.chinaunix.net/uid-205 ...

  4. [PythonCode]扫描局域网的alive ip地址

    内网的主机都是自己主动分配ip地址,有时候须要查看下有那些ip在使用,就写了个简单的脚本. linux和windows下都能够用,用多线程来ping1-255全部的地址,效率不高.2分钟左右. 先凑合 ...

  5. 怎样更好的设计你的REST API之基于REST架构的Web Service设计及REST框架实现

    一.REST 含状态传输(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 眼下在 ...

  6. 自动填充输入框 Asp .Net Mvc

    1 效果 当在一个文本框中输入时,可以自动查找相关选项,然后加载出来以供参考   2 前台代码   <link href="~/Content/themes/base/jquery-u ...

  7. 阿里云 oss 小文件上传进度显示

    对阿里云OSS上传小文件时的进度,想过两个方法:一是.通过多线程监測Inputstream剩余的字节数来计算,可是由于Inputstream在两个线程中共用,假设上传线程将Inputstream关闭, ...

  8. 在matlab中生成m序列

    实验环境为matlab2013b 1.首先编写一个mseq.m文件,内容为: function[mseq]=m_sequence(fbconnection)  n=length(fbconnectio ...

  9. 打造极致性能数据库中间件丨LVS+Keepalive+华为云DDM之理论篇

    背景说明 华为云分布式数据库中间件(Distributed Database Middleware,简称DDM),专注于解决数据库分布式扩展问题,突破了传统数据库的容量和性能瓶颈,实现海量数据高并发访 ...

  10. /etc/profile与/etc/bashrc、交互式与非交互式、login与non-login shell的差别

    线上的memcached又挂了.仍然没有得到core文件. 排查原因,同事发现启动memcached的脚本存在可疑问题. 问题一:没有设置memcached工作文件夹,有可能core dump时没有工 ...