|
| 1 | +//! A prefix tree (trie) that allows us to quickly search for common prefixes. |
| 2 | +//! |
| 3 | +//! Since the puzzle input only consists of lowercase characters from 'a' to |
| 4 | +//! 'z' and the towel patterns are on average rather short, I didn't bother |
| 5 | +//! implementing something sophisticated. A naive implementation where the |
| 6 | +//! nodes have an array of 26 pointers to their children is enough and does not |
| 7 | +//! consume too much memory. |
| 8 | +
|
| 9 | +#[derive(Debug, Default)] |
| 10 | +struct Node { |
| 11 | + children: [usize; 26], |
| 12 | + end: bool, |
| 13 | +} |
| 14 | + |
| 15 | +pub struct Trie { |
| 16 | + nodes: Vec<Node>, |
| 17 | +} |
| 18 | + |
| 19 | +impl Trie { |
| 20 | + pub fn default() -> Self { |
| 21 | + // insert root |
| 22 | + Trie { |
| 23 | + nodes: vec![Node::default()], |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /// Insert a prefix into the tree |
| 28 | + pub fn insert(&mut self, s: &str) { |
| 29 | + let mut current_node = 0; |
| 30 | + |
| 31 | + for c in s.chars() { |
| 32 | + debug_assert!( |
| 33 | + c.is_ascii_lowercase(), |
| 34 | + "This trie implementation can only ASCII lowercase characters" |
| 35 | + ); |
| 36 | + |
| 37 | + let i = (c as u8 - b'a') as usize; |
| 38 | + if self.nodes[current_node].children[i] == 0 { |
| 39 | + self.nodes[current_node].children[i] = self.nodes.len(); |
| 40 | + self.nodes.push(Node::default()); |
| 41 | + } |
| 42 | + |
| 43 | + current_node = self.nodes[current_node].children[i]; |
| 44 | + } |
| 45 | + |
| 46 | + self.nodes[current_node].end = true; |
| 47 | + } |
| 48 | + |
| 49 | + /// Look for prefixes of the given string and return their lengths. If |
| 50 | + /// there is no prefix, an empty Vec will be returned. |
| 51 | + pub fn common_prefix_lengths(&self, s: &str) -> Vec<usize> { |
| 52 | + let mut result = Vec::new(); |
| 53 | + let mut current_node = 0; |
| 54 | + |
| 55 | + for (len, c) in s.chars().enumerate() { |
| 56 | + debug_assert!( |
| 57 | + c.is_ascii_lowercase(), |
| 58 | + "This trie implementation can only ASCII lowercase characters" |
| 59 | + ); |
| 60 | + |
| 61 | + if self.nodes[current_node].end { |
| 62 | + result.push(len); |
| 63 | + } |
| 64 | + |
| 65 | + let i = (c as u8 - b'a') as usize; |
| 66 | + if self.nodes[current_node].children[i] == 0 { |
| 67 | + // nothing else to find |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + current_node = self.nodes[current_node].children[i]; |
| 72 | + } |
| 73 | + |
| 74 | + if self.nodes[current_node].end { |
| 75 | + result.push(s.len()); |
| 76 | + } |
| 77 | + |
| 78 | + result |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::Trie; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn common_prefix_lengths() { |
| 88 | + let mut t = Trie::default(); |
| 89 | + t.insert("foo"); |
| 90 | + t.insert("foobar"); |
| 91 | + t.insert("bar"); |
| 92 | + t.insert("bra"); |
| 93 | + t.insert("foobarfoo"); |
| 94 | + t.insert("fool"); |
| 95 | + t.insert("foofoo"); |
| 96 | + t.insert("foono"); |
| 97 | + t.insert("other"); |
| 98 | + |
| 99 | + assert_eq!(t.common_prefix_lengths("fo"), vec![]); |
| 100 | + assert_eq!(t.common_prefix_lengths("foo"), vec![3]); |
| 101 | + assert_eq!(t.common_prefix_lengths("fool"), vec![3, 4]); |
| 102 | + assert_eq!(t.common_prefix_lengths("foofoo"), vec![3, 6]); |
| 103 | + assert_eq!(t.common_prefix_lengths("foobar"), vec![3, 6]); |
| 104 | + assert_eq!(t.common_prefix_lengths("foobarbar"), vec![3, 6]); |
| 105 | + assert_eq!(t.common_prefix_lengths("foobarfoo"), vec![3, 6, 9]); |
| 106 | + assert_eq!(t.common_prefix_lengths("foobarfool"), vec![3, 6, 9]); |
| 107 | + } |
| 108 | +} |
0 commit comments