给定一个由N 个正整数组成的数组arr[]和一个长度为(N – 1)的字符串S ,包含字符‘+’和‘*’ ,任务是找到应用算术运算后可以获得的最小数字字符串S 中提到的数组元素以任意顺序排列。
例子:
Input: A[] ={2, 2, 2, 2}, S = “**+”
Output: 8
Explanation:
Operation 1: Perform the multiplication operation on the first two elements operation i.e., arr[0]*arr[1] = 2*2 = 4. Now the array modifies to {4, 2, 2}.
Operation 2: Perform the multiplication operation on the remaining two elements i.e., arr[1]*arr[2] = 2*2 = 4. Now the array modifies to {4, 4}.
Operation 3: Perform the addition operation on the remaining elements arr[0] + arr[1] = 4 + 4 = 8. Now the array modifies to {8}.
Therefore, the result of the operation performed is 8, which is minimum.
Input: A[] = {1, 2, 3, 4}, S = “*++”
Output: 9
方法:可以使用位掩码解决给定的问题。请按照以下步骤解决问题:
- 将字符串S中的乘法和加法运算的计数存储在变量中,分别为mul和add 。
- 现在,应用于数组元素的操作可以在掩码(二进制字符串)中进行编码,这样如果掩码的第i 位被设置,则它等于‘1’ ,并且必须执行乘法运算。否则,执行加法。
- 初始化一个变量,表示如ANS INT_MAX该存储结果的最小值。
- 因此,创建[0, 2 (N – 1) ]范围内的所有掩码并找到掩码中设置的位数并执行以下步骤:
- 如果掩码中设置的位数等于mul ,则将此掩码应用于数组A[] ,即,如果掩码的第i 位为“1”,则对第i个元素执行乘法运算和(i + 1)第元素。
- 否则,执行加法。
- 将ans的值更新为ans和上述步骤中计算出的值中的最小值。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the smallest number
// that can be obtained after applying
// the arithmetic operations mentioned
// in the string S
int minimumSum(int A[], int N, string S)
{
// Stores the count of multiplication
// operator in the string
int mul = 0;
for (int i = 0;
i < (int)S.size(); i++) {
if (S[i] == '*')
mul += 1;
}
// Store the required result
int ans = 1000000;
// Iterate in the range to
// create the mask
for (int i = 0;
i < (1 << (N - 1)); i++) {
int cnt = 0;
vector v;
// Checking the number of bits
// that are set in the mask
for (int j = 0; j < N - 1; j++) {
if ((1 << j) & (i)) {
cnt += 1;
v.push_back('*');
}
else {
v.push_back('+');
}
}
// Check if the number of bits
// that are set in the mask is
// multiplication operation
if (cnt == mul) {
// Storing the elements
// that is to be added
deque d;
d.push_back(A[0]);
// Apply the multiplications
// operation first
for (int j = 0; j < N - 1; j++) {
// If sign is '*', then
// multiply last element
// of deque with arr[i]
if (v[j] == '*') {
int x = d.back();
d.pop_back();
x = x * A[j + 1];
// Push last multiplied
// element in the deque
d.push_back(x);
}
else {
// If the element is to
// be added, then add
// it to the deque
d.push_back(A[j + 1]);
}
}
int sum = 0;
// Add all the element of
// the deque
while (d.size() > 0) {
int x = d.front();
sum += x;
d.pop_front();
}
// Minimize the answer with
// the given sum
ans = min(ans, sum);
}
}
return ans;
}
// Driver Code
int main()
{
int A[] = { 2, 2, 2, 2 };
string S = "**+";
int N = sizeof(A) / sizeof(A[0]);
cout << minimumSum(A, N, S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the smallest number
// that can be obtained after applying
// the arithmetic operations mentioned
// in the String S
static int minimumSum(int A[], int N, String S)
{
// Stores the count of multiplication
// operator in the String
int mul = 0;
for(int i = 0;
i < (int)S.length(); i++)
{
if (S.charAt(i) == '*')
mul += 1;
}
// Store the required result
int ans = 1000000;
// Iterate in the range to
// create the mask
for(int i = 0;
i < (1 << (N - 1)); i++)
{
int cnt = 0;
Vector v = new Vector();
// Checking the number of bits
// that are set in the mask
for(int j = 0; j < N - 1; j++)
{
if (((1 << j) & (i)) > 0)
{
cnt += 1;
v.add('*');
}
else
{
v.add('+');
}
}
// Check if the number of bits
// that are set in the mask is
// multiplication operation
if (cnt == mul)
{
// Storing the elements
// that is to be added
LinkedList d = new LinkedList();
d.add(A[0]);
// Apply the multiplications
// operation first
for(int j = 0; j < N - 1; j++)
{
// If sign is '*', then
// multiply last element
// of deque with arr[i]
if (v.get(j) == '*')
{
int x = d.getLast();
d.removeLast();
x = x * A[j + 1];
// Push last multiplied
// element in the deque
d.add(x);
}
else
{
// If the element is to
// be added, then add
// it to the deque
d.add(A[j + 1]);
}
}
int sum = 0;
// Add all the element of
// the deque
while (d.size() > 0)
{
int x = d.peek();
sum += x;
d.removeFirst();
}
// Minimize the answer with
// the given sum
ans = Math.min(ans, sum);
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 2, 2, 2, 2 };
String S = "**+";
int N = A.length;
System.out.print(minimumSum(A, N, S));
}
}
// This code is contributed by shikhasingrajput
8
时间复杂度: O(2 (N – 1) *N)
辅助空间: O(N)