给定一个长度相等的括号序列。任务是从给定的长度2和4的序列中找到有多少种方法来制作平衡括号子序列。
序列()是长度为2的括号序列。子序列是可以通过删除一些元素或不删除任何元素而无需更改其余元素的顺序而从另一个序列派生的序列。
注意: “ 1”代表左括号,“ 2”代表右括号
例子:
Input: 1 2 1 1 2 2
Output: 14
Input: 1 2 1 2
Output: 4
方法:对于长度为2的子序列,我们将仅对每个1看到多少个2,这可以通过对序列中存在的2的数目进行简单的后缀求和而轻松实现。因此,首先取序列中存在的2的后缀数之和。
对于长度为4的子序列,有2个选择:
First one is 1 1 2 2
And the other one is 1 2 1 2.
- 对于第一个,从左到右运行一个循环,以获取第一个开括号,然后从后缀数组中获得一个内循环,以获取下一个开括号,我们可以在第二个开括号之后得到计数2并计算子序列数用count *(count-1)/ 2表示,因为对于内部开口支架的每个闭合支架,我们得到第一个开口支架的count-1个选择数。
- 对于第二种子序列,我们再次从左到右运行一个循环,以获取第一个开放括号,而内部循环则获取下一个开放括号。然后,我们通过简单地减去第二个开括号之后的2的计数和第一个开括号之后的2的计数并将其乘以第二个开括号之后的2的计数,来获得第一个开括号之后的2的计数来计算子序列数。第二个开括号(我们从频率后缀数组中获得所有这些值)。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
void countWays(int a[], int n)
{
int i, j;
long suff[n];
if (a[n - 1] == 2)
suff[n - 1] = 1;
// Taking the frequncy suffix sum of the
// number of 2's present after every index
for (i = n - 2; i >= 0; i--)
{
if (a[i] == 2)
suff[i] = suff[i + 1] + 1;
else
suff[i] = suff[i + 1];
}
// Storing the count of subsequence
long ss = 0;
// Subsequence of length 2
for (i = 0; i < n; i++)
{
if (a[i] == 1)
ss += suff[i];
}
// Subsequence of length 4 of type 1 1 2 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1 && suff[j] >= 2)
{
ss += (suff[j]) * (suff[j] - 1) / 2;
}
}
}
// Subsequence of length 4 of type 1 2 1 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1
&& (suff[i] - suff[j]) >= 1
&& suff[j] >= 1)
{
ss += (suff[i] - suff[j]) * suff[j];
}
}
}
cout << (ss);
}
// Driver code
int main()
{
int a[] = { 1, 2, 1, 1, 2, 2 };
int n = 6;
countWays(a, n);
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java implementation of above approach
class GFG {
public static void countWays(int a[], int n)
{
int i, j;
long suff[] = new long[n];
if (a[n - 1] == 2)
suff[n - 1] = 1;
// Taking the frequncy suffix sum of the
// number of 2's present after every index
for (i = n - 2; i >= 0; i--)
{
if (a[i] == 2)
suff[i] = suff[i + 1] + 1;
else
suff[i] = suff[i + 1];
}
// Storing the count of subsequence
long ss = 0;
// Subsequence of length 2
for (i = 0; i < n; i++)
{
if (a[i] == 1)
ss += suff[i];
}
// Subsequence of length 4 of type 1 1 2 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1
&& suff[j] >= 2)
{
ss += (suff[j]) * (suff[j] - 1) / 2;
}
}
}
// Subsequence of length 4 of type 1 2 1 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1
&& (suff[i] - suff[j]) >= 1
&& suff[j] >= 1)
{
ss += (suff[i] - suff[j]) * suff[j];
}
}
}
System.out.println(ss);
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 2, 1, 1, 2, 2 };
int n = 6;
countWays(a, n);
}
}
Python 3
# Python 3 implementation of
# above approach
def countWays(a, n):
suff = [0] * n
if (a[n - 1] == 2):
suff[n - 1] = 1
# Taking the frequncy suffix sum
# of the number of 2's present
# after every index
for i in range(n - 2, -1, -1):
if (a[i] == 2):
suff[i] = suff[i + 1] + 1
else:
suff[i] = suff[i + 1]
# Storing the count of subsequence
ss = 0
# Subsequence of length 2
for i in range(n):
if (a[i] == 1):
ss += suff[i]
# Subsequence of length 4 of type 1 1 2 2
for i in range(n):
for j in range(i + 1, n):
if (a[i] == 1 and
a[j] == 1 and suff[j] >= 2):
ss += (suff[j]) * (suff[j] - 1) // 2
# Subsequence of length 4
# of type 1 2 1 2
for i in range(n):
for j in range(i + 1, n):
if (a[i] == 1 and a[j] == 1 and
(suff[i] - suff[j]) >= 1 and
suff[j] >= 1):
ss += (suff[i] - suff[j]) * suff[j]
print(ss)
# Driver Code
if __name__ == "__main__":
a = [1, 2, 1, 1, 2, 2]
n = 6
countWays(a, n)
# This code is contributed
# by ChitraNayal
C#
// C# implementation of
// above approach
using System;
class GFG
{
public static void countWays(int[] a, int n)
{
int i, j;
long[] suff = new long[n];
if (a[n - 1] == 2)
suff[n - 1] = 1;
// Taking the frequncy suffix
// sum of the number of 2's
// present after every index
for (i = n - 2; i >= 0; i--)
{
if (a[i] == 2)
suff[i] = suff[i + 1] + 1;
else
suff[i] = suff[i + 1];
}
// Storing the count of subsequence
long ss = 0;
// Subsequence of length 2
for (i = 0; i < n; i++)
{
if (a[i] == 1)
ss += suff[i];
}
// Subsequence of length 4
// of type 1 1 2 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1
&& suff[j] >= 2) {
ss += (suff[j]) * (suff[j] - 1) / 2;
}
}
}
// Subsequence of length 4
// of type 1 2 1 2
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] == 1 && a[j] == 1
&& (suff[i] - suff[j]) >= 1
&& suff[j] >= 1) {
ss += (suff[i] - suff[j]) * suff[j];
}
}
}
Console.WriteLine(ss);
}
// Driver Code
public static void Main()
{
int[] a = { 1, 2, 1, 1, 2, 2 };
int n = 6;
countWays(a, n);
}
}
// This code is contributed by Shashank
Javascript
输出
14
时间复杂度: O(N * N)
辅助复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。