POJ-3295 Tautology (构造)

https://vjudge.net/problem/POJ-3295

题意

有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1?

 

分析

首先明确各运算符的意义,K(&&),A(||),N(!),E(==),C特殊判断一下。计算的话肯定是从后往前的,遇到变量进栈,遇到运算符则出栈做运算。现在的问题是各个变量的数值未知,由于只有5个变量,总共只有32总可能,所以暴力跑一遍即可,每种取值都试一次。

 

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>

#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)

using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
    char c;int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1000000000;
int T;

void testcase(){
    printf("Case %d:",++T);
}

const int MAXN = 5e5+10 ;
const int MAXM = 150;
const double eps = 1e-8;
const double PI = acos(-1.0);

int p,q,r,s,t;
int sta[120];
char ss[120];

void run(){
    int top=0;
    int len = strlen(ss);
    int a,b;
    for(int i=len-1;i>=0;i--){
        if(ss[i]=='p') sta[top++]=p;
        else if(ss[i]=='q') sta[top++]=q;
        else if(ss[i]=='r') sta[top++]=r;
        else if(ss[i]=='s') sta[top++]=s;
        else if(ss[i]=='t') sta[top++]=t;
        else if(ss[i]=='K'){
            a=sta[--top];
            b=sta[--top];
            sta[top++]=(a&&b);
        }else if(ss[i]=='A'){
            a=sta[--top];
            b=sta[--top];
            sta[top++]=(a||b);
        }else if(ss[i]=='N'){
            a=sta[--top];
            sta[top++]=(!a);
        }else if(ss[i]=='C'){
            a=sta[--top];
            b=sta[--top];
            if(a==1&&b==0) sta[top++]=0;
            else sta[top++]=1;
        }else if(ss[i]=='E'){
            a=sta[--top];
            b=sta[--top];
            sta[top++]=(a==b);
        }
    }
}

bool work(){
    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++){
                        run();
                        if(sta[0]==0) return false;
                    }
    return true;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
//    init();
    while(~scanf("%s",ss)){
        if(ss[0]=='0') break;
        if(work()) puts("tautology");
        else puts("not");
    }
    return 0;
}

 

posted @ 2018-06-14 16:52  litos  阅读(153)  评论(0编辑  收藏  举报