From 8798fc1596d1ccbd7fb60ecc0d4927d5b6b4ff21 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 18 Dec 2024 10:01:39 +0800 Subject: [PATCH] 20241218 finished. --- src/problem/mod.rs | 2 + ...mber_of_valid_strings_to_form_target_ii.rs | 90 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/problem/p3292_minimum_number_of_valid_strings_to_form_target_ii.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index d2f0398..3a2f8ef 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -376,3 +376,5 @@ mod p1338_reduce_array_size_to_the_half; mod p1847_closest_room; mod p3291_minimum_number_of_valid_strings_to_form_target_i; + +mod p3292_minimum_number_of_valid_strings_to_form_target_ii; diff --git a/src/problem/p3292_minimum_number_of_valid_strings_to_form_target_ii.rs b/src/problem/p3292_minimum_number_of_valid_strings_to_form_target_ii.rs new file mode 100644 index 0000000..d922839 --- /dev/null +++ b/src/problem/p3292_minimum_number_of_valid_strings_to_form_target_ii.rs @@ -0,0 +1,90 @@ +/** + * [3292] Minimum Number of Valid Strings to Form Target II + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn min_valid_strings(words: Vec, target: String) -> i32 { + let words: Vec> = words.into_iter().map(|x| x.chars().collect()).collect(); + let target: Vec = target.chars().collect(); + + let calculate_prefix = |word: &Vec, target: &Vec| { + let s: Vec = word + .iter() + .chain(['#'].iter()) + .chain(target.iter()) + .map(|x| x.clone()) + .collect(); + let mut result = vec![0; s.len()]; + + // KMP to calculate prefix array + for i in 1..s.len() { + let mut j = result[i - 1]; + while j > 0 && s[i] != s[j] { + j = result[j - 1]; + } + + if s[i] == s[j] { + j += 1; + } + result[i] = j; + } + + result + }; + + let n = target.len(); + let mut back = vec![0; n]; + for word in words.iter() { + let pi = calculate_prefix(word, &target); + let m = word.len(); + + for i in 0..n { + back[i] = back[i].max(pi[m + 1 + i]); + } + } + + let mut dp = vec![0; n + 1]; + + for i in 1..=n { + dp[i] = 1e9 as i32; + } + + for i in 0..n { + dp[i + 1] = dp[i + 1 - back[i]] + 1; + if dp[i + 1] > n as i32 { + return -1; + } + } + + dp[n] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3292() { + assert_eq!( + 3, + Solution::min_valid_strings( + vec_string!("abc", "aaaaa", "bcdef"), + "aabcdabc".to_owned() + ) + ); + assert_eq!( + 2, + Solution::min_valid_strings(vec_string!("abababab", "ab"), "ababaababa".to_owned()) + ); + assert_eq!( + -1, + Solution::min_valid_strings(vec_string!("abcdef"), "xyz".to_owned()) + ); + } +}