From 0bf4ebd4f73f03fdc536f01591723dd07b5ea00f Mon Sep 17 00:00:00 2001 From: Ariel Date: Tue, 9 Jan 2024 17:18:10 +0800 Subject: [PATCH] Create trie-d.rs --- trees/trie-d.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 trees/trie-d.rs diff --git a/trees/trie-d.rs b/trees/trie-d.rs new file mode 100644 index 0000000..8132a64 --- /dev/null +++ b/trees/trie-d.rs @@ -0,0 +1,73 @@ +// lc-2707.rs + +struct Solution; + +use std::collections::HashMap; + +struct Trie { + mark: bool, + nxt: HashMap>, +} + +impl Trie { + fn new() -> Self { + Self {mark: false, nxt: HashMap::new()} + } + fn insert(&mut self, s: Vec) { + let mut ptr = self; + for x in s.into_iter().rev() { + ptr = ptr.nxt.entry(x).or_insert(Box::new(Trie{mark: false, nxt: HashMap::new()})); + } + ptr.mark = true; + } + fn query(&self, s: &Vec) -> Vec { + let mut ptr = self; + let mut res = vec![]; + for (i, &x) in s.iter().rev().enumerate() { + if ptr.mark { + res.push(i); + } + if let Some(p) = ptr.nxt.get(&x) { + ptr = p.as_ref(); + } else { + return res; + } + } + if ptr.mark { + res.push(s.len()); + } + res + } +} + +impl Solution { + pub fn min_extra_char(s: String, dictionary: Vec) -> i32 { + let s = s.into_bytes(); + let n = s.len(); + let mut trie = Trie::new(); + for word in dictionary { + trie.insert(word.into_bytes()); + } + // eprintln!("{:?}", trie.query(&("leetcode".to_string().into_bytes()))); + // eprintln!("{:?}", trie.query(&("code".to_string().into_bytes()))); + let mut dp = vec![0;n+1]; + let mut query_string = vec![]; + for i in 1..=n { + query_string.push(s[i-1]); + let matchings = trie.query(&query_string); + dp[i] = (0..i).fold(usize::MAX, |c, j|c.min(i - j + dp[j])); + dp[i] = dp[i].min(matchings.into_iter().fold(usize::MAX, |c,j|c.min(dp[i-j]))); + } + dp[n] as i32 + } +} + +fn main() { + let mut buf = String::new(); + std::io::stdin().read_line(&mut buf).ok(); + let s: String = buf.trim().parse::().unwrap(); + buf.clear(); + std::io::stdin().read_line(&mut buf).ok(); + let dictionary: Vec = buf.trim().split(' ').map(|x|x.parse::().unwrap()).collect::>(); + println!("{}", Solution::min_extra_char(s, dictionary)); +}