Compute the length of the longest common subsequence of two line slices. Uses the classic DP approach with O(min(m,n)) space.
(a: &[&str], b: &[&str])
| 328 | /// Compute the length of the longest common subsequence of two line slices. |
| 329 | /// Uses the classic DP approach with O(min(m,n)) space. |
| 330 | fn lcs_length(a: &[&str], b: &[&str]) -> u64 { |
| 331 | let (short, long) = if a.len() <= b.len() { (a, b) } else { (b, a) }; |
| 332 | let n = short.len(); |
| 333 | let mut prev = vec![0u64; n + 1]; |
| 334 | let mut curr = vec![0u64; n + 1]; |
| 335 | |
| 336 | for long_line in long { |
| 337 | for (j, short_line) in short.iter().enumerate() { |
| 338 | if long_line == short_line { |
| 339 | curr[j + 1] = prev[j] + 1; |
| 340 | } else { |
| 341 | curr[j + 1] = curr[j].max(prev[j + 1]); |
| 342 | } |
| 343 | } |
| 344 | std::mem::swap(&mut prev, &mut curr); |
| 345 | curr.iter_mut().for_each(|v| *v = 0); |
| 346 | } |
| 347 | |
| 348 | *prev.last().unwrap_or(&0) |
| 349 | } |
| 350 | |
| 351 | impl Default for RevertManager { |
| 352 | fn default() -> Self { |
no outgoing calls
no test coverage detected