POJ 3295 Tautology 构造 stack

本文探讨了如何通过解析给定的逻辑表达式来判断其是否为逻辑恒等式,涉及栈操作、布尔逻辑运算符的应用及边界条件处理。

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

简单模拟,我把变量少看了一个Runtime Error到死,还以为是哪里错了,蒟蒻都是这样orz。


ACcode:

#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%lf%lf",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
string str;
int len;
bool flag;
int fun(int p,int q,int r,int s,int t){
    stack<int>st;
    //cout<<p<<' '<<q<<' '<<s<<' '<<t<<"     ";
    for(int i=len-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]=='N'){
            int t=st.top();st.pop();
            st.push(!t);
        }else if(str[i]=='K'){
            int x=st.top();st.pop();
            int w=st.top();st.pop();
            st.push(w==1&&x==1);
        }else if(str[i]=='A'){
            int x=st.top();st.pop();
            int w=st.top();st.pop();
            st.push(w||x);
        }
        else if(str[i]=='C'){
            int x=st.top();st.pop();
            int w=st.top();st.pop();
            st.push(!(w==1&&x==0));
        }else if(str[i]=='E'){
            int x=st.top();st.pop();
            int w=st.top();st.pop();
            st.push(w==x);
        }
    }
   // cout<<st.top()<<"\n------------------------\n";
    return st.top();
}
int main(){
    while(cin>>str){
        if(str[0]=='0')
            break;
        len=str.length();
        flag=true;
        FOR(p,0,1)
            FOR(q,0,1)
                FOR(r,0,1)
                    FOR(s,0,1)
                        FOR(t,0,1)
                            if(!fun(p,q,r,s,t)){
                                //cout<<"asdasdadas\n";
                                flag=false;
                                break;
                            }
        printf(flag?"tautology\n":"not\n");
    }
    return 0;
}
/**
ApNp
ApNq
0
**/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值