This commit is contained in:
william 2023-06-04 20:26:40 -04:00
parent c124d6ccdb
commit d793297ad5
2 changed files with 118 additions and 37 deletions

View File

@ -1,22 +1,21 @@
use std::cmp::max; use std::cmp::max;
use std::str::Chars; use std::str::Chars;
use crate::matrix::Matrix; use crate::matrix::Matrix;
pub fn diff(a: &str, b: &str) { pub fn diff(a: &str, b: &str) {
let m = build_matrix(a, b); 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<i32> { fn build_matrix(a: &str, b: &str) -> Matrix<i32> {
let mut a = a.chars(); let mut m = Matrix::new(a.len(), b.len(), 0i32);
let mut b = b.chars();
let mut m = Matrix::new(*(&a.count()), *(&b.count()), 0i32);
for i in 0..a.count() { let mut i = 0;
for j in 0..b.count() { let mut j = 0;
let x = a.nth(i).unwrap();
let y = b.nth(j).unwrap();
for x in a.chars() {
for y in b.chars() {
let v = if x == y { let v = if x == y {
if i == 0 || j == 0 { if i == 0 || j == 0 {
0 0
@ -30,45 +29,79 @@ fn build_matrix(a: &str, b: &str) -> Matrix<i32> {
}; };
m.set(v, i, j).unwrap(); m.set(v, i, j).unwrap();
j += 1;
} }
i += 1;
j = 0;
} }
m m
} }
fn print_diff(m: Matrix<i32>, a: &mut Chars, b: &mut Chars, i: usize, j: usize) { fn print_diff(m: Matrix<i32>, a: &str, b: &str, i: usize, j: usize) {
if i < 0 && j < 0 { let char_i = a.chars().nth(i).unwrap();
println!(); let char_j = b.chars().nth(j).unwrap();
return;
}
let char_i = a.nth(i).unwrap(); println!("A: {char_i}, B: {char_j}");
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;
}
if char_i == char_j { if char_i == char_j {
print_diff(m, a, b, i - 1, j - 1);
println!(" {char_i}"); println!(" {char_i}");
return; return;
} }
if m.get(i, j - 1).unwrap() >= m.get(i - 1, j).unwrap() { if i > 0 {
print_diff(m, a, b, i, j - 1); let up = m.get(i - 1, j).unwrap();
println!("+ {char_j}");
if j > 0 && m.get(i, j - 1).unwrap() >= up {
println!("+ {char_i}");
return; return;
} }
}
print_diff(m, a, b, i - 1, j);
println!("- {char_i}"); println!("- {char_i}");
} }
// fn print_diff(m: Matrix<i32>, 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}");
// }

View File

@ -4,9 +4,57 @@ mod matrix;
mod lcs; mod lcs;
fn main() { fn main() {
let a = "let a = \"ABCDE\";"; let a = "abcd";
let b = "let b = \"ABCDE\";"; 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);
}