Boolean Satisability

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

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;}
在编程语言中,"Boolean" 和 "boolean" 通常指的是相同的概念,即表示逻辑上的真(true)或假(false)。不过,它们的使用方式可能会因编程语言的不同而有所差异。 ### Boolean(布尔类型) "Boolean" 是大多数编程语言中的一种数据类型,用于表示布尔值。它有两个可能的值:`true`(真)和 `false`(假)。布尔类型常用于条件判断和逻辑运算。 #### 示例(JavaScript): ```javascript let isTrue = true; let isFalse = false; if (isTrue) { console.log("这是真的"); } else { console.log("这是假的"); } ``` #### 示例(Java): ```java boolean isTrue = true; boolean isFalse = false; if (isTrue) { System.out.println("这是真的"); } else { System.out.println("这是假的"); } ``` ### boolean(布尔值) 在某些语言中,如 Java,布尔类型的声明关键字是 `boolean`,而首字母大写的 `Boolean` 可能是该类型的包装类。例如,在 Java 中,`boolean` 是基本数据类型,而 `Boolean` 是一个类,它可以持有 `null` 值。 #### 示例(Java): ```java Boolean isTrue = true; // 使用 Booleanboolean isFalse = false; // 使用基本数据类型 boolean System.out.println(isTrue); // 输出 true System.out.println(isFalse); // 输出 false ``` ### 总结 - `boolean` 通常是指编程语言中的基本数据类型。 - `Boolean` 可能是该类型的包装类(如在 Java 中),它可以持有 `null` 值,而基本类型 `boolean` 不能。 不同语言可能有不同的约定和实现,因此在具体使用时需要参考相应语言的文档。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值