poj3295 Tautology

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/small__snail__5/article/details/79963337

//题意:p,q,r,s,t分别带别可0可1的数字,A(w,x),N,E(w,x),C(w,x),K(w,x),分别代表逻辑运算分别为或、非、是否相等、非w或x、与;当然这里的w,x只是举例实则是pqrst,然后目的给你一个传操作若是永真式输出一个tautology,否则输出not

//思路:自己构造一个栈,因为每种操作数都是在操作符后面的,从右向左入栈方便操作简单具体见代码(【注意】有个很奇怪的地方刚开始觉得state数组可以开10个本以为,后面发现小了,但是他报的错误是TLE让我无限懵逼了很久到现在也没想懂为什么)

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

char str[200];
int state[100];
int p, q, r, s, t;
int str_len;

void calculate()
{
    int top = 0;
    for(int i = str_len - 1; i >= 0; i--){
        if(str[i] == 'p'){
            state[top++] = p;
        } else if(str[i] == 'q'){
            state[top++] = q;
        } else if(str[i] == 'r'){
            state[top++] = r;
        } else if(str[i] == 's'){
            state[top++] = s;
        } else if(str[i] == 't'){
            state[top++] = t;
        } else if(str[i] == 'K'){
            state[top-2] = state[top-2] & state[top-1];
            top--;
        } else if(str[i] == 'A'){
            state[top-2] = state[top-2] | state[top-1];
            top--;
        } else if(str[i] == 'N'){
            state[top-1] = !state[top-1];
        } else if(str[i] == 'C'){
            state[top-2] = !state[top-1] | state[top-2];
            top--;
        } else if(str[i] == 'E'){
            state[top-2] = state[top-2] == state[top-1] ? 1 : 0;
            top--;
        }
    }
}

bool solve()
{
    bool ans;
    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++){
                        calculate();
                        if(state[0] == 0)
                            return false;
                    }
                }
            }
        }
    }
    return true;
}

int main()
{
    while(scanf("%s", str) != EOF){
        str_len = strlen(str);
        if(strcmp(str, "0") == 0)
            break;
        if(solve()){
            printf("tautology\n");
        } else {
            printf("not\n");
        }
    }
    return 0;
}
阅读更多

没有更多推荐了,返回首页