Logical Expression(C++)

题目描述:

Given are N strings S1,...,SN,each of which is AND or OR.

Find the number of tuples of N+1 variables (x0​,…,xN​), where each element is True  or False, such that the following computation results in yN being True:

  • y0​=x0​;
  • for i≥1, yi​=yi−1​∧xi​ if Si​ is AND, and yi​=yi−1​∨xi​ if Si​ is OR.

Here, a∧b and a∨b are logical operators.

题目大意:

给N个字符串,不是“AND”就是“OR”。找能够满足以下条件使yN=True的所有可能的元组(x0,...,xN),其中每个元素不是“True”就是“False”:

y0=x0;

如果字符串Si是“AND”   yi = y(i-1) xi ,i=1,2,...

如果字符串Si是“OR”   yi = y(i-1) xi,i=1,2,...

在这里∧ v 是逻辑运算(数学里的“且”和“或”)

条件限制:

  • 1≤N≤60
  • Si​ is AND or OR.

输入格式:

Input is given from Standard Input in the following format:

N
S1​
.
.
.
SN

输出格式:

Print the answer.

实例:

输入1:

2
AND
ORs

输出1:

5

这是提示:

For example, if (x0​,x1​,x2​)=(True,False,True), we have y2​=True, as follows:

  • y0​=x0​=True
  • y1​=y0​∧x1​=True∧False=False
  • y2​=y1​∨x2​=False∨True=True

All of the five tuples (x0​,x1​,x2​) resulting in y2​=True are shown below:

  • (True,True,True)
  • (True,True,False)
  • (True,False,True)
  • (False,True,True)
  • (False,False,True)

输入2:

5
OR
OR
OR
OR
OR

输出2:

63

False result in y5​=True.

解题思路:

首先:“且”——除了True ∧ True=True  其他情况都是False

           “或”——除了False v False = False 其他情况都是True

然后:递归,两个两个判断,把每一步中的可能元组用一个res[N][2]的数组记录下来,且令

          res[0][0]=res[0][1]=1,res[N][0]记录最后推导出结果为False的元组可能的个数,res[N][1]记

          录最后推导出结果为True的元组可能的个数。最后输出 res[N][1] (要的是结果是True的情况)

举个例子:就用输入1的情况

                  先是AND 现在res数组里有一个False一个True(res[0][0]=res[0][1]),用“且”运算,则

                  res[1][0] = res[0][0]*2 + res[0][1] = 3(就是(False,False);(False,True);   

               (True;False)这三种结果为假的可能)所以  res[1][1] = res[0][1] = 1

                 之后是OR 在上面的基础上,用“或”运算,则

                 res[2][0] = res[1][0] = 3((False,False)这一种,但是前一个False有三种组合)

                 res[2][1] = res[1][0] + res[1][1]*2 = 5

结论

//AND情况
res[i][0] = res[i - 1][0]*2 + res[i - 1][1];//False
res[i][1] = res[i - 1][1];//True

//OR情况
res[i][0] = res[i - 1][0];//False
res[i][1] = res[i - 1][1] * 2 + res[i - 1][0];//True

参考代码:

#include<iostream>
#include<string>
using namespace std;
#define int long long
const int N = 3e5 + 5;
int a[N];
int res[N][2];
int n;
signed main()
{
	string s;
	cin >> n;
    //记录是“且”还是“或”运算
	for (int i = 1; i <= n; i++)
	{
		cin >> s;
		if (s[0] == 'A')
			a[i] = 1;
		else if (s[0] == 'O')
			a[i] = 2;
	}
    //开始逐个计算
	res[0][0] = res[0][1] = 1;
	for (int i = 1; i <= n; i++)
	{
		if (a[i] == 1)//AND
		{
			res[i][0] = res[i - 1][0]*2 + res[i - 1][1];
			res[i][1] = res[i - 1][1];
		}
		else if (a[i] == 2)//OR
		{
			res[i][0] = res[i - 1][0];
			res[i][1] = res[i - 1][1] * 2 + res[i - 1][0];
		}
	}
	cout << res[n][1] << endl;//输出结果
	return 0;
}

这个真的好难表达出来,希望可以对你有些帮助!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值