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 ...
随机推荐
- html5开发手机打电话发短信功能
原文:http://www.open-open.com/code/view/1449843459332 在很多的手机网站上,有打电话和发短信的功能,对于这些功能是如何实现的呢.其实不难,今天我们就用h ...
- zz年度热门编程语言排行榜
原文在这里:Link 蛮有意思的,可以看看.
- 【APUE】进程间通信之消息队列
三种IPC的共同特征 1.标识符和键 每个内核中的IPC结构都用一个非负整数的标识符加以引用.当一个IPC结构被创建,以后又被删除时,与这种结构相关的标识符连续加1,直至达到一个整型数的最大值,然后又 ...
- JVM原理及内存溢出
JVM原理及内存溢出
- vs2010下配置CUDA出现kernel launch failed问题,内核无效
首先, 推荐一篇不错的配置文档~手把手教你 CUDA 5.5与VS2010编译环境的搭建.笔者就是在这篇文章的指导下成功地在VS2010上搭建了CUDA 6.5~ 其次. 文末给出的执行演示样例不好使 ...
- Java经常使用日期操作具体解释
Date类型大多数时间分量计算方法已经被Calendar代替 Date经常用法setTime getTime() new Date();默认获取当前的时间 SimpleDateFormat用来格式化和 ...
- Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能
前面学了Trie,那么就即学即用.运用Trie数据结构来解决这道题目. 本题目比較简单,当然能够不使用Trie.只是多用高级数据结构还是非常有优点的. 题目: Vova is fond of anim ...
- jQuery 1.x and 2.x , which is better?
1. jQuery 1.x和2.x的区别 或者可以说是jQuery 2.x有什么新特征? jQuery官方发布2.x原话 不再支持IE6/7/8,如果在IE9/10里只用“兼容性视图”模式也将会受到影 ...
- Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find resource 'solrconfig.xml' in classpath or 'D:\solr\solr-7.2.1\server\solr\new_core'
\solr-7.2.1\server\solr\configsets\_default 下的conf 复制到: \solr-7.2.1\server\solr\new_core
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...