POJ 3295-Tautology(构造法+栈)

Tautology

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9936   Accepted: 3774

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 Well-formed formula (WFF) is any string of these symbols obeying the following rules:

  • p, q, r, s, and t are WFFs
  • if w is a WFF, Nw is a WFF
  • if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.

The meaning of a WFF is defined as follows:

  • p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
  • K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E
     w  x   Kwx   Awx    Nw   Cwx   Ewx
  1  1   1   1    0   1   1
  1  0   0   1    0   0   0
  0  1   0   1    1   1   0
  0  0   0   0    1   1   1

tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the
value 0 for p=0, q=1.

You must determine whether or not a WFF is a tautology.

Input

Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

Output

For each test case, output a line containing tautology or not as appropriate.

Sample Input

ApNp
ApNq
0

Sample Output

tautology
not

题意:输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,

其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;

K、A、N、C、E为逻辑运算符,

K --> and:  x && y

A --> or:  x || y

N --> not :  !x

C --> implies :  (!x)||y

E --> equals :  x==y

问这个逻辑表达式是否为永真式。

思路:WFF的计算方法:从字符串WFF的末尾开始依次向前读取字符。构造一个栈stack,当遇到逻辑变量 p, q, r, s ,t 则将其当前的值压栈;遇到 N 则取栈顶元素进行非运算,运算结果的值压栈;遇到K, A, C, E则从栈顶中弹出两个元素进行相应的运算,将结果的值压栈。 由于输入是合法的,当字符串WFF扫描结束时,栈stack中只剩一个值,该值就是逻辑表达式WFF的值。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
char str[110];
stack<int>Q;
int p,q,r,s,t,len;
int IN_stack()
{
        int i;
        int x,y;
        for(i=strlen(str)-1; i>=0; i--) {
                if(str[i] == 'p')
                        Q.push(p);
                else if(str[i] == 'q')
                        Q.push(q);
                else if(str[i] == 'r')
                        Q.push(r);
                else if(str[i] == 's')
                        Q.push(s);
                else if(str[i] == 't')
                        Q.push(t);
                else if(str[i]== 'K') {
                        x = Q.top();
                        Q.pop();
                        y = Q.top();
                        Q.pop();
                        Q.push(x && y);
                } else if(str[i] == 'A') {
                        x = Q.top();
                        Q.pop();
                        y = Q.top();
                        Q.pop();
                        Q.push(x || y);
                } else if(str[i] == 'N') {
                        x = Q.top();
                        Q.pop();
                        Q.push(!x);
                } else if(str[i] == 'C') {
                        x = Q.top();
                        Q.pop();
                        y = Q.top();
                        Q.pop();
                        Q.push((!x)||y);
                } else if(str[i] == 'E') {
                        x = Q.top();
                        Q.pop();
                        y = Q.top();
                        Q.pop();
                        Q.push(x == y);
                }

        }
}
int judge()
{
        for(p=0; p<2; p++)
                for(q=0; q<2; q++)
                        for(r=0; r<2; r++)
                                for(s=0; s<2; s++)
                                        for(t=0; t<2; t++) {
                                                IN_stack();
                                                if(Q.top()==0)
                                                        return 0;
                                        }
        return 1;

}
int main()
{
        while(~scanf("%s",str)) {
                getchar();
                if(str[0] == '0')
                        break;
                if(judge()) {
                        printf("tautology\n");
                } else printf("not\n");
        }
        return 0;
}
时间: 2024-08-13 02:25:09

POJ 3295-Tautology(构造法+栈)的相关文章

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

[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 (构造)

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

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 [ 栈 ]

传送门 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 伪递归

题目链接: 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,

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

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

poj 3295 Tautology

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

Poj 3295

用的枚举的办法.列表上写的是构造法. WA了很长时间.开始的时候发现了一些没有赋初值的情况(继承了上次的值),后来不知道怎么回事了.结果是因为一个数组,p[i],i是ASCii码,结果我的范围写的i是100,导致了奇怪的错误.为何没有溢出CE? // #includes {{{ // #includes {{{ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #