diff --git a/src/lcs.rs b/src/lcs.rs index c1c714b..fd1984c 100644 --- a/src/lcs.rs +++ b/src/lcs.rs @@ -1,22 +1,21 @@ use std::cmp::max; use std::str::Chars; + use crate::matrix::Matrix; pub fn diff(a: &str, b: &str) { let m = build_matrix(a, b); - print_diff(m, &mut a.chars(), &mut b.chars(), a.len() - 1, b.len() - 1); + print_diff(m, a, b, a.len() - 1, b.len() - 1); } fn build_matrix(a: &str, b: &str) -> Matrix { - let mut a = a.chars(); - let mut b = b.chars(); - let mut m = Matrix::new(*(&a.count()), *(&b.count()), 0i32); + let mut m = Matrix::new(a.len(), b.len(), 0i32); - for i in 0..a.count() { - for j in 0..b.count() { - let x = a.nth(i).unwrap(); - let y = b.nth(j).unwrap(); + let mut i = 0; + let mut j = 0; + for x in a.chars() { + for y in b.chars() { let v = if x == y { if i == 0 || j == 0 { 0 @@ -30,45 +29,79 @@ fn build_matrix(a: &str, b: &str) -> Matrix { }; m.set(v, i, j).unwrap(); + + j += 1; } + + i += 1; + j = 0; } m } -fn print_diff(m: Matrix, a: &mut Chars, b: &mut Chars, i: usize, j: usize) { - if i < 0 && j < 0 { - println!(); - return; - } +fn print_diff(m: Matrix, a: &str, b: &str, i: usize, j: usize) { + let char_i = a.chars().nth(i).unwrap(); + let char_j = b.chars().nth(j).unwrap(); - let char_i = a.nth(i).unwrap(); - let char_j = b.nth(j).unwrap(); - - if i < 0 { - print_diff(m, a, b, i, j - 1); - println!("+ {char_i}"); - return; - } - - if j < 0 { - print_diff(m, a, b, i - 1, j); - println!("- {char_j}"); - return; - } + println!("A: {char_i}, B: {char_j}"); if char_i == char_j { - print_diff(m, a, b, i - 1, j - 1); println!(" {char_i}"); return; } - if m.get(i, j - 1).unwrap() >= m.get(i - 1, j).unwrap() { - print_diff(m, a, b, i, j - 1); - println!("+ {char_j}"); - return; + if i > 0 { + let up = m.get(i - 1, j).unwrap(); + + if j > 0 && m.get(i, j - 1).unwrap() >= up { + println!("+ {char_i}"); + return; + } } - print_diff(m, a, b, i - 1, j); println!("- {char_i}"); -} \ No newline at end of file +} + +// fn print_diff(m: Matrix, a: &str, b: &str, i: usize, j: usize) { +// // if i < 0 && j < 0 { +// // println!(); +// // return; +// // } +// +// let char_i = a.chars().nth(i).unwrap(); +// let char_j = b.chars().nth(j).unwrap(); +// +// // if i < 0 { +// // print_diff(m, a, b, i, j - 1); +// // println!("+ {char_i}"); +// // return; +// // } +// // +// // if j < 0 { +// // print_diff(m, a, b, i - 1, j); +// // println!("- {char_j}"); +// // return; +// // } +// +// if char_i == char_j { +// if i > 0 && j > 0 { +// print_diff(m, a, b, i - 1, j - 1); +// } +// +// println!(" {char_i}"); +// return; +// } +// +// if i > 0 && j > 0 && m.get(i, j - 1).unwrap() >= m.get(i - 1, j).unwrap() { +// print_diff(m, a, b, i, j - 1); +// println!("+ {char_j}"); +// return; +// } +// +// if i > 0 { +// print_diff(m, a, b, i - 1, j); +// } +// +// println!("- {char_i}"); +// } diff --git a/src/main.rs b/src/main.rs index 864f08f..e068d8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,57 @@ mod matrix; mod lcs; fn main() { - let a = "let a = \"ABCDE\";"; - let b = "let b = \"ABCDE\";"; + let a = "abcd"; + let b = "abce"; - diff(a, b); + // diff(a, b); + lcs(a, b); } +fn lcs(a: &str, b: &str) { + let n = a.len(); + let m = b.len(); + let max = (n + m) / 2; + let mut v = vec![0usize; max * 2]; + + for d in 0..max { + let mut k = 0usize; + while k <= d * 2 { + let mut x = if k == 0 || k != d * 2 && v[k - 1] < v[k + 1] { + v[k + 1] + } else { + v[k - 1] + 1 + }; + + let mut y = if k < x { + x - k + } else { + 0 + }; + + while x < n && y < m { + let ac = a.chars().nth(x + 1).unwrap(); + let bc = b.chars().nth(y + 1).unwrap(); + + if ac != bc { + break; + } + + x = x + 1; + y = y + 1; + } + + v[k] = x; + if x >= n && y >= m { + println!("Length of a SES is D ({d})"); + dbg!(v); + return; + } + + k += 2; + } + } + + println!("Length of a SES is greater than MAX ({max})"); + dbg!(v); +}