POJ3295-Tautology

題目鏈接:點擊打開鏈接

題目大意是根據給出的運算法則,判斷式子是否真,變量有5個,枚舉所有可能值,其中有假就爲假。

//164K	0MS	
//C++	1899B
#include <cstdio>
#include <cstring>
#define abs(i) (i>=0?i:-i)
int p,q,r,s,t;
char str[111];
int st[111];
void x()
{
    int top=0;
    int len=strlen(str);
    for(int i=len-1;i>=0;i--)
    {
        if(str[i]=='p') st[top++]=p;
        else if(str[i]=='q') st[top++]=q;
        else if(str[i]=='r') st[top++]=r;
        else if(str[i]=='s') st[top++]=s;
        else if(str[i]=='t') st[top++]=t;
        else if(str[i]=='K')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1&&t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='A')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1||t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='C')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1==1&&t2==0)
                st[top++]=0;
            else
                st[top++]=1;
        }
        else if(str[i]=='E')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1==t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='N')
        {
            int t1=st[--top]-1;
            st[top++]=abs(t1);
        }
    }
}
bool f()
{
    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++)
                    {
                        x();
                        if(st[0]==0)
                            return false;
                    }
    return true;
}
int main()
{
    while(~scanf("%s",str)&&strcmp(str,"0"))
    {
        if(f())
        {
            printf("tautology\n");
        }
        else
        {
            printf("not\n");
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章