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.
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:

输入
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.
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;}