poj-3295 Tautology

http://poj.org/problem?id=3295

借助的是大神的解题报告的写法。

大致题意:

输入由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

问这个逻辑表达式是否为永真式。

PS:输入格式保证是合法的。

代码操作:

1、把变量(p,q,r,s,t)替换成0,1。

(替换有规律。见数组num。共32中可能);

2、用栈将字符串倒序当后缀式运算。

3、推断运算完毕后栈底元素是否为1,假设不为0跳出输出not。

4、若32种可能都为1。则输出tautology;

 

#include<string.h>
#include<stdio.h>
const int maxn=120;
int sta[maxn]; //数组模拟堆栈
char str[maxn];
int p,q,r,s,t;
void doit()
{
    int top=0;
    int len=strlen(str);
    for(int i=len-1;i>=0;i--)
    {
        if(str[i]=='p') sta[top++]=p;
        else if(str[i]=='q') sta[top++]=q;
        else if(str[i]=='r') sta[top++]=r;
        else if(str[i]=='s') sta[top++]=s;
        else if(str[i]=='t') sta[top++]=t;
        else if(str[i]=='K')
        {
            int t1=sta[--top];
            int t2=sta[--top];
            sta[top++]=(t1&&t2);
        }
        else if(str[i]=='A')
        {
            int t1=sta[--top];
            int t2=sta[--top];
            sta[top++]=(t1||t2);
        }
        else if(str[i]=='N')
        {
            int t1=sta[--top];
            sta[top++]=(!t1);
        }
        else if(str[i]=='C')
        {
            int t1=sta[--top];
            int t2=sta[--top];
            if(t1==1&&t2==0) sta[top++]=0;
            else sta[top++]=1;
        }
        else if(str[i]=='E')
        {
            int t1=sta[--top];
            int t2=sta[--top];
            if((t1==1&&t2==1)||(t1==0&&t2==0)) sta[top++]=1;
            else sta[top++]=0;
        }
    }
}

bool solve()
{   //5重循环,枚举2^5  32种可能 假设都满足 return 1 
    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++)
                    {
                        doit();
                        if(sta[0]==0)return 0;
                    }
     return 1;
}

int main()
{
    while(scanf("%s",&str))
    {
        if(strcmp(str,"0")==0)break;
        if(solve()) printf("tautology\n");
        else printf("not\n");
    }
    return 0;
}


 

转载于:https://www.cnblogs.com/ldxsuanfa/p/9954873.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值