题目链接:

  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. 报错: The type ByteInputStream is not accessible due to restriction on required library

    报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...

  2. how to read openstack code: Core plugin and resource extension

    本章我们将写一个自己的core plugin 和一个resource extension来加深理解.(阅读本文的前提是你已经理解了restful以及stevedore等内容) 什么是 core plu ...

  3. eclipse发布项目到tomcat部署目录

    1.在eclipse下建立Dynamic Web Project工程zhgy,在使用eclipse中new一个tomcat,通过启动该tomcat来发布Dynamic Web Project的时候,其 ...

  4. TList实现的任务队列

    TList实现的任务队列 var g_tasks: TList; type PTRecvPack = ^TRecvPack; TRecvPack = record // 接收到的原数据 socket: ...

  5. arcgis安装路径的获得

    //Get the ArcGIS install location string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path; / ...

  6. 配置Python 2.7.1外加环境pywin32-216.win32-py2.7

    python-2.7.1  安装包 下载地址:http://download.csdn.net/detail/baidu_14854543/7985187 pywin32-216.win32-py2. ...

  7. Linux文件系统与磁盘管理

    Linux文件系统与磁盘管理 有哪些文件系统: FAT:微软在Dos/Windows系列操作系统中共使用的一种文件系统的总称.       exFAT(Extended File Allocation ...

  8. SQLServer导出单表数据

    采用生成脚本---仅数据..   如果是部分数据,可以先把部分数据备份到一个表中 select * into .. from ...

  9. vs2010 创建和发布 webservice

    1 打开VS2010,菜单    文件->新建->项目 2 选择[ASP.net 空web应用程序],将其命名为自己想的工程名称. 3 右键点击工程,添加->新建项 选择 web服务 ...

  10. A - BBQ Easy

    Score : 200 points Problem Statement Snuke is having a barbeque party. At the party, he will make N  ...