poj 3295 Tautology 伪递归

题目链接:

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

题目描述:

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

解题思路:

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

 1 #include <cstdio>//本代码用G++交就ac,c++交就wa,因为G++是从前向后求值的,c++是从后向前求值的,
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 #define maxn 105
 8 char str[maxn];
 9 int index;
10
11 bool dfs (int num);//对表达式求值
12 bool judge ();//对变量的取值进行枚举
13 int main ()
14 {
15     while (scanf ("%s", str), strcmp(str, "0"))
16     {
17         if (judge ())
18             printf ("tautology\n");
19         else
20             printf ("not\n");
21     }
22     return 0;
23 }
24 bool dfs (int num)
25 {
26     index ++;
27     if (str[index] == ‘p‘)
28         return num & 1;
29     else if (str[index] == ‘q‘)
30         return (num >> 1) & 1;
31     else if (str[index] == ‘r‘)
32         return (num >> 2) & 1;
33     else if (str[index] == ‘s‘)
34         return (num >> 3) & 1;
35     else if (str[index] == ‘t‘)
36         return (num >> 4) & 1;
37     else if (str[index] == ‘K‘)
38         return dfs (num) & dfs (num);//这里的& | 不能用&& ||,因为后者使用时前面的为假就会停止运算,不能达到预计的效果
39     else if (str[index] == ‘A‘)
40         return dfs (num) | dfs (num);
41     else if (str[index] == ‘N‘)
42         return !dfs (num);
43     else if (str[index] == ‘C‘)
44         return !dfs(num) | dfs(num);
45     else if (str[index] == ‘E‘)
46         return dfs (num) == dfs(num);
47
48 }
49 bool judge ()
50 {
51     int i;
52     for (i=0; i<32; i++)
53     {
54         index = -1;
55         if (!dfs (i))
56             return 0;
57     }
58     return 1;
59 }
时间: 2024-08-12 14:30:28

poj 3295 Tautology 伪递归的相关文章

[ACM] POJ 3295 Tautology (构造)

Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s,

POJ - 3295 - Tautology = 枚举 + 二叉树遍历

http://poj.org/problem?id=3295 题意:给若干个小写字母表示bool变量,大写字母表示bool运算,求这个表达式的是否为永真表达式. 输入形如: ApNp ApNq 也就是前缀表达式. 所以就写个东西遍历它构造一棵树,然后给同名变量枚举赋值,假如没有任何赋值使得树根输出0,则为永真表达式. 考察二叉树的递归遍历. #include<algorithm> #include<cmath> #include<cstdio> #include<

POJ 3295 Tautology (栈模拟)

Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10096   Accepted: 3847 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s

POJ 3295 Tautology(构造法)

题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13231   Accepted: 5050 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the po

poj 3295 Tautology [ 栈 ]

传送门 Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10107   Accepted: 3850 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q,

poj 3295 Tautology

这题属于构造题,有两点需要想到: 1.所谓永真式就是说题设中所给的五个逻辑变量无论如何取值,最终的运算结果总是为真,这里就需要枚举五个逻辑变量的取值情形.很容易想到共有32种情况: 也就是说,对于每个字符串,如果这32种情况下的运算结果都是真的话, 那它就是永真式.1~32, 用按位与的办法,可以逐次枚举这32中情况. 2.运算顺序:可以这样理解题目中的字符串结构,它就像一颗树,所以叶子节点做逻辑运算之后逐次汇聚到根节点得到最后的结果.于是想到从尾部开始(也就是从叶节点向上). 如果遇到逻辑变量

POJ - 3295 - Tautology (构造)

题目传送:Tautology 思路:枚举所有变量可能的值(0或1),算出其表达式的值,因为题目是要求是否是永真式,求式子的真值可以用栈来求,栈的话,可以自己构造一个栈,也可以用STL的stack AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #in

[ACM] POJ 3295 Ubiquitous Religions (并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted: 11379 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

Tautology - poj 3295

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10437   Accepted: 3963 Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Wel