poj 3295 Tautology 伪递归
题目链接:
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 伪递归的更多相关文章
- 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 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- 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
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- 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: 8127 Accepted: 3115 ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
随机推荐
- arcgis安装路径的获得
//Get the ArcGIS install location string sInstall = ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Path; / ...
- Linux信号通讯编程
信号通讯流程为: ①进程A/内核选择信号 ②发送信号 ③进程B接收信号并处理 Linux系统支持的全部信号均定义在/usr/include/asm/signal.h.当中常见的信号有: ①SIGKIL ...
- 【转】c++ 如何批量初始化数组 fill和fill_n函数的应用
http://blog.csdn.net/sunquana/article/details/9153213 一. fill和fill_n函数的应用: fill函数的作用是:将一个区间的元素都赋予val ...
- Mapreduce运行过程分析(基于Hadoop2.4)——(三)
4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()->ReduceTask.run().ReduceTask.run方 ...
- 获取Windows用户所有的账户名
/// <summary> /// 设置用户密码 /// </summary> [DllImport("Netapi32.dll")] extern sta ...
- 【转载】C# 装箱和拆箱[整理]
1. 装箱和拆箱是一个抽象的概念 2. 装箱是将值类型转换为引用类型 :拆箱是将引用类型转换为值类型 利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的 ...
- 在学习c++过程中,总结类的三个用户以及使用权限,感觉非常实用
首先我们需要知道类的三个用户分别是:类的实现者,类的普通用户和类的继承者(派生类),接下来分别讲解这几种用户的区别. 1 .类的实现者:顾明思议,就是类的设计者,拥有最大的权限,可以访问类中任何权限的 ...
- Javascript中的apply、call、bind
apply . call .bind 三者都是用来改变函数的this对象的指向的: apply . call .bind 三者第一个参数都是this要指向的对象,也就是想指定的上下文: apply . ...
- cygwin安装sshd服务(win7)Error installing a service: OpenSCManager: Win32 error 5:
Error installing a service: OpenSCManager: Win32 error 5: 出现这个问题的解决办法:win7系统管理员运行Cygwin软件 ...
- ubuntu中查看已安装软件包的方法
ubuntu中查看已安装软件包的方法: 方法一:在新立得软件包管理器中,打到已安装,便可以看看有多少包被安装. 如果想把这些包的信息复制到一文件里,可用下面的方法. 方法二:在终端输入 sudo dp ...