Tautology

Tautology

 

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
题意:
离散题,如果是永真式,则输出tautology,否则输出not
解题思路:
从后往前判断,枚举+栈思想
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
	int stack[200];
	char str[200];
	int p,q,r,s,t;
	while(scanf("%s",&str)&&str[0]!='0')
	{
		int flag=1;
		for(p=0;p<2&&flag;p++)
			for(q=0;q<2&&flag;q++)
				for(r=0;r<2&&flag;r++)
					for(s=0;s<2&&flag;s++)
						for(t=0;t<2&&flag;t++)
						{
							int top=0;
							for(int i=strlen(str)-1;i>=0;i--)
							{
								if(str[i]=='p')stack[top++]=p;
								else if(str[i]=='q')stack[top++]=q;
								else if(str[i]=='r')stack[top++]=r;
								else if(str[i]=='s')stack[top++]=s;
								else if(str[i]=='t')stack[top++]=t;
								else if(str[i]=='K')
								{
									int ta=stack[--top];
									int tb=stack[--top];
									stack[top++]=ta&&tb;
								}
								else if(str[i]=='A')
								{
									int ta=stack[--top];
									int tb=stack[--top];
									stack[top++]=ta||tb;
								}
								else if(str[i]=='N')
								{
									int ta=stack[--top];
									stack[top++]=!ta;
								}
								else if(str[i]=='C')
								{
									int ta=stack[--top];
									int tb=stack[--top];
									if(ta==1&&tb==0)
										stack[top++]=0;
									else 
										stack[top++]=1;
								}
								else if(str[i]=='E')
								{
									int ta=stack[--top];
									int tb=stack[--top];
									if(ta==tb)
										stack[top++]=1;
									else
										stack[top++]=0;
								}
								
							}
							if(stack[0]==0)
								flag=0;
						}
		if(flag)
			printf("tautology\n");
		else
			printf("not\n");
	}
	return 0;
}


#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
    char str[200];
    while(cin>>str&&str[0]!='0')
    {
       int p,q,r,s,t;
       int flag=1;
       for(p=0;p<2&&flag;p++)
            for(q=0;q<2&&flag;q++)
                for(r=0;r<2&&flag;r++)
                    for(s=0;s<2&&flag;s++)
                        for(t=0;t<2&&flag;t++)
                        {
                            stack<int >st;
                            for(int i=strlen(str)-1;i>=0;i--)
                            {
                                if(str[i]=='p')
                                    st.push(p);
                                else if(str[i]=='q')
                                    st.push(q);
                                else if(str[i]=='r')
                                    st.push(r);
                                else if(str[i]=='s')
                                    st.push(s);
                                else if(str[i]=='t')
                                    st.push(t);
                                else if(str[i]=='K')
                                {
                                    int ta=st.top();
                                    st.pop();
                                    int tb=st.top();
                                    st.pop();
                                    st.push(ta&&tb);
                                }
                                else if(str[i]=='A')
                                {
                                    int ta=st.top();
                                    st.pop();
                                    int tb=st.top();
                                    st.pop();
                                    st.push(ta||tb);
                                }
                                else if(str[i]=='N')
                                {
                                    int ta=st.top();
                                    st.pop();
                                    st.push(!ta);
                                }
                                else if(str[i]=='C')
                                {
                                    int ta=st.top();
                                    st.pop();
                                    int tb=st.top();
                                    st.pop();
                                    if(ta==1&&tb==0)
                                        st.push(0);
                                    else
                                        st.push(1);
                                }
                                else if(str[i]=='E')
                                {
                                    int ta=st.top();
                                    st.pop();
                                    int tb=st.top();
                                    st.pop();
                                    if(ta==tb)
                                        st.push(1);
                                    else
                                        st.push(0);
                                }
                            }
                            flag=st.top();
                        }
        if(flag)
            cout<<"tautology\n";
        else
            cout<<"not\n";
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
重言式判断是指判断一个逻辑表达式是否为永真式(即无论变量的取值如何,表达式都为真)。在数据结构中,可以使用二叉树来表示逻辑表达式,并通过遍历二叉树来计算表达式的值。 以下是判断重言式的步骤: 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、付费专栏及课程。

余额充值