Foundations of Fortune left class8- title element portion 8 and the array equal to a value

1. Title: some elements of the array and

To give you an array arr, and an integer aim. If you can select any number of arr, AIM can get accumulated, returns true or false

2. Violence recursive

(1) Analysis

Ideas and sequences 8-3 print string the same for each element can be selected or not, a recursive continuously forming a large tree, the leaf nodes is the result of the determination of all the elements. f () represents a function of the current position is the first of several elements, and the second parameter represents the current. f (0,0) represents the initial position and the recording is not started 0, f (2,5) represents the current in the second position, and 5.
Here Insert Picture Description
End condition is beyond the current position of the array (each element exhaustive over end), intermediate process still have to record the current and a sum.

(2) the core code

						//当前数 ,当前和,目标 
bool isSum(vector<int> v,int i, int sum, int aim)
{
	if(i == v.size())
	{
		return sum == aim;
	} 
//	if(sum > aim)	return false;
	return isSum(v,i + 1,sum,aim) || isSum(v,i + 1,sum + v[i],aim);
} 

3. Dynamic Programming

(1) Analysis

① Whether there aftereffect
Here Insert Picture Description
For both cases, 3,2, 3,2 do not, and 5, to 5, the transfer function f is f (3,5) Description No aftereffect.
②dp array
The title given array, the target aim are the same, only the position i and sum changes to i, sum two-dimensional array dp axis established
Here Insert Picture Description
analysis: i represents the number of elements in the array + 1, first, because why add also need not start the (0,0) position enumerated, sum is an array of all elements. Then the new array dp [i] [J] means the i-th position is the current and j, dp entire array contains all possibilities. For sure at 0 aim - between the sum, by the recursion conditions of this problem, the final leaf node is the desired result, then (the last one), not the aim of the row labeled 0 on the leaf node, said they did not comply with F, alone and just the middle column and the aim is expressed as 1 aim.
Reanalysis anywhere dp [i] [j], represents the current number of discarded (j constant) and dp [i] recursively in accordance with the conditions depend on the current position dp [i + 1] [j ] [j + v [i]] plus the current number is reflected in the current row of the array depends on the following two lines of position, a position is vertically downward, a displacement current is below the location of a number, as long as a and two as return aim 1 .

The following goal is to fill the array, below the upper position depends on the position, aim the known position of the last row is 1, the remainder is 0, the second number of rows can be pushed down, and finally return to the launch position 0,0.
Here Insert Picture Description

(2) the core code

int isSum2(vector<int>& v, int sum, int aim)
{																	//把sum缩减为aim长度,相当于减枝 
	vector<vector<int> > dp(v.size() + 1,vector<int>(aim + 1,0));	//初始化长度加一因为包括了0,0位置 
	
	for (int i = 0; i < dp.size(); i++) 
	{
			dp[i][aim] = 1;											//因为依赖垂直的列 aim列值为1,
	}
	for(int i = dp.size() - 2;i >= 0; i--)
		for(int j = aim - 1;j >= 0;j--)
		{
			dp[i][j] = dp[i + 1][j]; 								//依赖垂直的列
			if(j + v[i] <= aim)										//如果另一个偏移的位置不超出数组范围 
				dp[i][j] = dp[i][j] || dp[i + 1][j + v[i]];			//两个位置有一个满足就可以 
		}
	return dp[0][0];
}

4. The complete code

#include<iostream>
#include<vector>
using namespace std;
//递归					//当前数 ,当前和,目标 
bool isSum(vector<int>& v,int i, int sum, int aim)
{
	if(i == v.size())
	{
		return sum == aim;
	} 
//	if(sum > aim)	return false;
	return isSum(v,i + 1,sum,aim) || isSum(v,i + 1,sum + v[i],aim);
} 
//动态规划 
int isSum2(vector<int>& v, int sum, int aim)
{																	//把sum缩减为aim长度,相当于减枝 
	vector<vector<int> > dp(v.size() + 1,vector<int>(aim + 1,0));	//初始化长度加一因为包括了0,0位置 
	
	for (int i = 0; i < dp.size(); i++) 
	{
			dp[i][aim] = 1;											//因为依赖垂直的列 aim列值为1,
	}
	for(int i = dp.size() - 2;i >= 0; i--)
		for(int j = aim - 1;j >= 0;j--)
		{
			dp[i][j] = dp[i + 1][j]; 								//依赖垂直的列
			if(j + v[i] <= aim)										//如果另一个偏移的位置不超出数组范围 
				dp[i][j] = dp[i][j] || dp[i + 1][j + v[i]];			//两个位置有一个满足就可以 
		}
	return dp[0][0];
}


int main()
{
	vector<int>v = {2,1,7,5};
//	vector<int>v = {12,14,33,45,66,45,33,2,5,4};
//	cout<<isSum(v,0,0,13);
	cout<<isSum2(v,0,13);

	return 0;
}
Published 51 original articles · won praise 1 · views 1359

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104835219