pub fn sequence_matcher(
keys: &mut Vec<i32>,
targets: &mut Vec<i32>,
max_key_length: usize,
max_target_length: usize,
n_candidates: usize,
use_all_keys: bool,
use_all_targets: bool,
) -> Result<Vec<AnswerElement>, String>Expand description
Finds the integers from two vectors that sum to the same value.
This method assumes that the two vectors have Many-to-Many relationships.
Each integer of the keys vector corresponds to the multiple integers of the targets vector.
With this method, we can find combinations of the integers.
To avoid combinatorial explosion, some parameters need to be set.
max_key_length is used to restrict the number of values in keys chosen.
If max_key_length is 3, an answer’s length is at most 3, such as [1980 + 2980 + 3500], [1050]
max_target_length is the same as max_key_length for targets.
n_candidates specifies the maximum number of pattern.
If use_all_keys is true, an answer must contain all the elements of the keys.
If use_all_targets is true, an answer must contain all the elements of the targets.
When both use_all_keys and use_all_targets are true, the sum of the keys and the targets must be the same.
§Arguments
keys- An array.targets- An array.max_key_length- An integer.max_target_length- An integer.n_candidates- An integer.use_all_keys- Boolean.use_all_targets- Boolean.
§Example
use dpss::dp::sequence_matcher;
let answer = sequence_matcher(&mut vec![1980, 2980, 3500, 4000, 1050], &mut vec![1950, 2900, 30, 80, 3300, 200, 3980, 1050, 20], 10, 10, 100, true, true).unwrap();
assert_eq!(answer[0].answer_arr, vec![
(vec![1050],
vec![1050]),
(vec![1980],
vec![30, 1950]),
(vec![2980],
vec![80, 2900]),
(vec![3500],
vec![200, 3300]),
(vec![4000],
vec![20, 3980]),
]);
assert_eq!(answer[1].answer_arr, vec![
(vec![1050],
vec![1050]),
(vec![1980],
vec![30, 1950]),
(vec![2980],
vec![80, 2900]),
(vec![3500, 4000],
vec![20, 200, 3300, 3980]),
]);