2023-05-28 21:43:20 -04:00
|
|
|
use std::cmp::min;
|
|
|
|
use std::io;
|
|
|
|
use crate::matrix::Matrix;
|
|
|
|
|
|
|
|
mod matrix;
|
|
|
|
|
|
|
|
const ADD_VAL: i32 = 1;
|
|
|
|
const MATCH_VAL: i32 = 0;
|
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
let str_a = "String A";
|
|
|
|
let str_b = "String B";
|
|
|
|
|
|
|
|
let mut diff_mat = Matrix::new(str_a.len(), str_b.len(), 0i32);
|
|
|
|
|
|
|
|
for col in 1..diff_mat.col_count {
|
|
|
|
let previous = diff_mat.get(0, col - 1).unwrap();
|
|
|
|
diff_mat.set(previous + ADD_VAL, 0, col).expect("Failed to initialize diff matrix");
|
|
|
|
}
|
|
|
|
|
|
|
|
for row in 1..diff_mat.row_count {
|
|
|
|
let previous = diff_mat.get(row - 1, 0).unwrap();
|
|
|
|
diff_mat.set(previous - ADD_VAL, row, 0).expect("Failed to initialize diff matrix");
|
|
|
|
}
|
|
|
|
|
|
|
|
for row in 1..diff_mat.row_count {
|
|
|
|
for col in 1..diff_mat.col_count {
|
|
|
|
let char_a = str_a.chars().nth(row).unwrap();
|
|
|
|
let char_b = str_b.chars().nth(col).unwrap();
|
|
|
|
|
|
|
|
let add_from = diff_mat.get(row, col - 1).unwrap();
|
|
|
|
let sub_from = diff_mat.get(row - 1, col).unwrap();
|
|
|
|
let substitute_from = diff_mat.get(row - 1, col - 1).unwrap();
|
|
|
|
|
|
|
|
let add = add_from + ADD_VAL;
|
|
|
|
let sub = sub_from - ADD_VAL;
|
|
|
|
let substitute = substitute_from + if char_a == char_b {
|
|
|
|
MATCH_VAL
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
|
|
|
|
let val = min(add, min(sub, substitute));
|
|
|
|
diff_mat.set(val, row, col).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diff_mat.print();
|
|
|
|
Ok(())
|
2023-05-27 11:27:49 -04:00
|
|
|
}
|