poj 3295 Tautology 构造法

本文介绍了一种通过解析逻辑公式来判断其是否为永真的算法实现。该算法使用堆栈结构处理逻辑运算符如与(K)、或(A)、非(N)、蕴含(C)和等于(E),并遍历所有变量取值的可能性来确定公式是否对于所有情况都成立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tautology

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6949 Accepted: 2653

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

View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <stack>
  4 #include <cstring>
  5 using namespace std;
  6 
  7 stack <int> s;
  8 
  9 void fk()
 10 {
 11     int a = s.top();
 12     s.pop();
 13     int b = s.top();
 14     s.pop();
 15     a &= b;
 16     s.push(a);
 17 }
 18 
 19 void fa()
 20 {
 21     int a = s.top();
 22     s.pop();
 23     int b = s.top();
 24     s.pop();
 25     a |= b;
 26     s.push(a);
 27 }
 28 
 29 void fc()
 30 {
 31     int a = s.top();
 32     s.pop();
 33     int b = s.top();
 34     s.pop();
 35     a = a<=b ? 1 : 0;
 36     s.push(a);
 37 }
 38 
 39 void fe()
 40 {
 41     int a = s.top();
 42     s.pop();
 43     int b = s.top();
 44     s.pop();
 45     a = (a==b) ? 1 : 0;
 46     s.push(a);
 47 }
 48 
 49 void fn()
 50 {
 51     int a = s.top();
 52     s.pop();
 53     s.push(!a);
 54 }
 55 
 56 int main()
 57 {
 58 //    freopen("in.txt","r",stdin);
 59     int k;
 60     int i;
 61     int a[5];
 62     char arr[1005];
 63     int flag;
 64     while (scanf("%s",arr))
 65     {
 66         flag = 1;
 67         if (arr[0] == '0') break;
 68         for (k=0; k<32; k++)
 69         {
 70             int temp = k;
 71             for (i=4; i>=0; i--)
 72             {
 73                 a[i] = temp%2;
 74                 temp = temp>>1;
 75             }
 76             int len = strlen(arr);
 77             for (i=len-1; i>=0; i--)
 78             {
 79                 if (arr[i] == 'K') fk();
 80                 else if (arr[i] == 'A') fa();
 81                 else if (arr[i] == 'N') fn();
 82                 else if (arr[i] == 'C') fc();
 83                 else if (arr[i] == 'E') fe();
 84                 else
 85                 {
 86                     s.push(a[arr[i]-'p']);
 87                 }
 88             }
 89             int tmp = s.top();
 90             if (tmp != 1)
 91             {
 92                 flag = 0;
 93                 break;
 94             }
 95         }
 96         if (flag)
 97             printf("tautology\n");
 98         else
 99             printf("not\n");
100     }
101     return 0;
102 }

 

转载于:https://www.cnblogs.com/shijianupc/archive/2013/01/21/2869392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值