标签:
| 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:
| 
 | 
| 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
题意:输入由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;
}
标签:
原文地址:http://blog.csdn.net/u013486414/article/details/42580691