Boolean Satisability

本文介绍了一个特定类型的布尔可满足性问题—1-DNF-SAT问题,并提供了解决方案。1-DNF-SAT问题关注如何计算1-DNF形式的布尔公式所有可能的真值赋值方式,以使其评估结果为真。

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

Boolean Satis ability

题目描述

Boolean satisfiability problem (SAT) is known to be a very hard problem in computer science. In this problem you are given a Boolean formula, and you need to find out if the variables of a given formula can be consistently replaced by the values true or false in such a way that the formula evaluates to true. 
SAT is known to be NP-complete problem. Moreover, it is NP-complete even in case of 3-CNF formula (3-SAT). However, for example, SAT problem for 2-CNF formulae (2-SAT) is in P.
#SAT is the extension of SAT problem. In this problem you need to check if it is possible, and count the number of ways to assign values to variables. This problem is known to be #P-complete even for 2-CNF formulae. We ask you to solve #1-DNF-SAT, which is #SAT problem for 1-DNF formulae.
You are given a Boolean formula in 1-DNF form. It means that it is a disjunction (logical or) of one or more clauses, each clause is exactly one literal, each literal is either variable or its negation (logical not). 
Formally:
Your task is to find the number of ways to replace all variables with values true and false (all occurrences of the same variable should be replaced with same value), such that the formula evaluates to true.

输入

The only line of the input file contains a logical formula in 1-DNF form (not longer than 1000 symbols).
Logical operations are represented by ‘|' (disjunction) and ‘~' (negation). The variables are ‘A'...Z' and ‘a'...‘z' (uppercase and lowercase letters are different variables). The formula contains neither spaces nor other characters not mentioned in the grammar.

输出

Output a single integer ---- the answer for #SAT problem for the given formula.

样例输入

B|~B

样例输出

2
题目大意:
输入一行字符,‘|’代表取并集,‘~’代表非,有从a到z(区分大小写)代表不同的命题,问最后使该式为真的真假赋值情况有几种
解题思路:
查看其中不同字符出现的种类数n,每两个不同种类的命题间的情况数都是2*2,答案就是2的n次幂种,如果有同一类字符同非,或者都同时不非,种类数减一。
特别判断只有一类字符的情况。
给出代码:
#include<stdio.h>
#include<string.h>
char maze[1010]={'\0'};
int a[1010][2],b[1010][2];
int main()
{	long long int l,i,countt,sum,flag;
	scanf("%s",maze);
	
	l=strlen(maze);
	memset(a,0,sizeof(a));  //代表小写字母  a[i][0]中储存该字符出现的不是非的情况
	memset(b,0,sizeof(b));  //代表大写字母	a[[i][1]中储存该字符出现的是非的情况
				
	flag=1;
	for(i=0;i<l;i++){
            if(maze[i]=='|')
                flag=1;
             else if(maze[i]=='~')
                flag=0;
else{if(maze[i]>='a'&&maze[i]<='z'){if(flag==1)a[(int)(maze[i]-'a')][0]=1;elsea[(int)(maze[i]-'a')][1]=1;}else{if(flag==1)b[(int)(maze[i]-'A')][0]=1;elseb[(int)(maze[i]-'A')][1]=1;}}} //确定字符出现的种类countt=0;flag=0;for(i=0;i<26;i++){if((a[i][0]==1&&a[i][1]==1)||(b[i][0]==1&&b[i][1]==1)){flag=1;if(a[i][0]==1&&a[i][1]==1)countt++;if(b[i][0]==1&&b[i][1]==1)countt++;}else{if(a[i][0]==1||a[i][1]==1)countt++;if(b[i][0]==1||b[i][1]==1)countt++;}} //flag来记录是否出现了一类字符同非,或者都不同非if(countt==1){if(flag==0)printf("1\n");elseprintf("2\n");}else{sum=1;for(i=0;i<countt;i++)sum*=2;if(flag==0)sum-=1;printf("%lld\n",sum);}return 0;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值