Tautology(POJ 3295)(暴力枚举)

题目链接:http://poj.org/problem?id=3295
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12454 Accepted: 4738
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
A 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
Source

Waterloo Local Contest, 2006.9.30

题解:
枚举p,q,r,s,t的可能值,如果枚举过程中有一个不行,那么就说明不行,直接退出。而实现方法是用栈,遇到运算符就把所需要的栈中元素压出来,遇到元素,就把相应的值压入栈。最后栈中剩下的元素就是结果。

思考:
1、一定要会算时间复杂度,如果时间复杂度可以,那么直接用暴力的算法就行,不用在题上浪费那么多时间
2、要学会用栈,因为这道题模拟的电脑操作,所以要想到栈

#include <iostream>
#include <string>
#include <stack>

using namespace std;

string s;

int OP (char c, int a, int b)
{
    if (c == 'K') return a & b;
    if (c == 'A') return a | b;
    if (c == 'N') return !a;
    if (c == 'C') return a <= b;
    if (c == 'E') return a == b;
}

int Count ( )
{
    int l = s.length();
    int a[10];
    for (a[0] = 0; a[0] <= 1; a[0]++) {
        for (a[1] = 0; a[1] <= 1; a[1]++) {
            for (a[2] = 0; a[2] <= 1; a[2]++) {
                for (a[3] = 0; a[3] <= 1; a[3]++) {
                    for (a[4] = 0; a[4] <= 1; a[4]++) {
                        stack<int> s1;
                        for (int i = l - 1; i >= 0; i--) {
                            if (s[i] == 'p' || s[i] == 'q' || s[i] == 'r' || s[i] == 's' || s[i] == 't') {
                                s1.push (a[s[i] - 'p']);
                            } else if (s[i] == 'N') {
                                int now = s1.top();
                                s1.pop();
                                now = !now;
                                s1.push(now);
                            } else {
                                int t1 = s1.top(); s1.pop();
                                int t2 = s1.top(); s1.pop();
                                s1.push (OP (s[i], t2, t1));
                            }
                            if (i == 0) {
                                int now = s1.top();
                                s1.pop();
                                if (now != 1) return 0;
                            }
                        }
                    }
                }
            }
        }
    }
    return 1;
}

int main()
{
    while (cin >> s)
    {
        if (s[0] == '0') break;
        int flag = Count ();
        if (flag) cout << "tautology" << endl;
        else cout << "not" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
重言判断是指判断一个逻辑表达是否为永真(即无论变量的取值如何,表达都为真)。在数据结构中,可以使用二叉树来表示逻辑表达,并通过遍历二叉树来计算表达的值。 以下是判断重言的步骤: 1. 构建二叉树:根据逻辑表达的结构,构建一棵二叉树。每个节点表示一个操作符或操作数,左子树和右子树分别表示操作符的两个操作数。 2. 遍历二叉树:使用递归或栈来遍历二叉树。对于每个节点,根据操作符的类型进行相应的计算。 - 如果是操作数节点,返回节点的取值(0或1)。 - 如果是非操作符节点(例如取反操作符~),对其子节点进行递归计算,并根据操作符的类型进行相应的取反操作。 - 如果是与操作符节点(&),对其左右子节点进行递归计算,并返回两个子节点的逻辑与结果。 - 如果是或操作符节点(|),对其左右子节点进行递归计算,并返回两个子节点的逻辑或结果。 3. 判断重言:如果遍历完整个二叉树后,得到的结果始终为真(即为1),则逻辑表达为重言;否则,逻辑表达不是重言。 下面是一个示例的二叉树表示和判断重言的代码: ```python class Node: def __init__(self, value): self.value = value self.left = None self.right = None def is_tautology(root): if root is None: return True if root.value == '~': return not is_tautology(root.right) if root.value == '&': return is_tautology(root.left) and is_tautology(root.right) if root.value == '|': return is_tautology(root.left) or is_tautology(root.right) return root.value # 构建二叉树 root = Node('&') root.left = Node('|') root.left.left = Node('A') root.left.right = Node('~A') root.right = Node('|') root.right.left = Node('B') root.right.right = Node('~B') # 判断重言 result = is_tautology(root) print("The expression is a tautology:", result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值