题目链接:

  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. android手机rootROM下载地址

    https://download.mokeedev.com/ https://download.lineageos.org/

  2. vue 自定义报警组件

    1.自定义报警组件 Alarm.vue <!-- 报警 组件 --> <template> <div class="alarm"> <!- ...

  3. C++第9周(春)项目5 - 一元一次方程类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...

  4. 在开发过程中,如何在手机上测试vue-cli构建的项目

    由于有时候谷歌手机调试与真是的手机环境还是有一定的差距,所以需要在手机上测试项目. 手机上测试vue-cli构建项目方法: 打开项目config/index.js文件,找到module.exports ...

  5. [CSAPP]Bufbomb实验报告

    Bufbomb实验报告 实验分析: level 0-3从test開始制运行,通过函数getbuf向外界读取一串内容(buf). Level 4 是通过參数-n,程序运行testn函数,调用getbuf ...

  6. cocos2dx 3.0 显示中文及乱码解决方式

    遇到此问题第一时间在脑子里面联想到android下的strings.xml来做国际化,本文就仅仅针对解析xml来实现cocos2d-x的国际化解决乱码问题. 寻找解决方法的时候在cocos2d-x的c ...

  7. 使用PowerShell 创建SharePoint 站点

    使用PowerShell 创建SharePoint 站点         在SharePoint开发中,你应该学会使用PowerShell管理SharePoint网站.SharePoint Manag ...

  8. Python爬虫开发【第1篇】【多线程爬虫及案例】

    糗事百科爬虫实例: 地址:http://www.qiushibaike.com/8hr/page/1 需求: 使用requests获取页面信息,用XPath / re 做数据提取 获取每个帖子里的用户 ...

  9. 网易新闻client(高仿)

    近期整理了下自己曾经做过的项目,决定分享出来.本篇所展示的是仿网易新闻client,服务端是在新浪SAE部署着的.所以大家下载后,可直接在手机上看到效果.接下来看效果图: watermark/2/te ...

  10. java基础入门-建立能够多client链接的ServerSocket

    承接上一篇文章,今天谈论一下能够多client链接的ServerSocket. 这里面注意涉及到的技术点是: 1.ServerSocket 2.多线程 这次我们分成两个类来实现,先上代码: packa ...