#[allow(non_snake_case)]
#[cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))]
pub fn lcs(orig: &str, edit: &str, split: &str) -> (i32, String) {
let a: Vec<&str> = orig.split(split).collect();
let b: Vec<&str> = edit.split(split).collect();
let N = a.len() as i32;
let M = b.len() as i32;
let MAX = N + M;
let mut v: Vec<i32> = (-MAX..MAX).collect();
let mut common = String::new();
v[1] = 0;
for D in 0..MAX {
let mut max = 0;
let mut max_snake: Box<String> = Box::new("".to_string());
let mut k = -D;
while k < D + 1 {
let mut snake = String::new();
let mut x;
let index = (MAX + k - 1) as usize;
if k == -D || k != D && v[index - 1] < v[index + 1] {
x = v[index + 1];
} else {
x = v[index - 1] + 1;
}
let mut y = x - k;
while x < N && y < M && a[x as usize] == b[y as usize] {
if !snake.is_empty() {
snake.push_str(split);
}
snake.push_str(a[x as usize]);
x += 1;
y += 1;
}
v[index] = x;
if x > max {
max = x;
max_snake = Box::new(snake);
}
if x >= N && y >= M {
if max_snake.len() > 0 {
if !common.is_empty() {
common.push_str(split);
}
common.push_str(&max_snake);
} else {
common.push_str(split);
}
return (D, common);
}
k += 2;
}
if max_snake.len() > 0 {
if !common.is_empty() {
common.push_str(split);
}
common.push_str(&max_snake);
}
}
(MAX, "".to_string())
}
#[test]
fn test_lcs() {
assert_eq!(lcs("test", "tost", ""), (2, "tst".to_string()));
assert_eq!(lcs("test", "test", ""), (0, "test".to_string()));
assert_eq!(lcs("test", "test", " "), (0, "test".to_string()));
assert_eq!(lcs("The quick brown fox jumps over the lazy dog",
"The quick brown dog leaps over the lazy cat",
""),
(16, "The quick brown o ps over the lazy ".to_string()));
assert_eq!(lcs("The quick brown fox jumps over the lazy dog",
"The quick brown dog leaps over the lazy cat",
" "),
(6, "The quick brown over the lazy ".to_string()));
assert_eq!(lcs("The quick brown fox jumps over the lazy dog",
"The quick brown dog leaps over the lazy cat",
"\n"),
(2, "".to_string()));
assert_eq!(lcs("The quick brown fox jumps over the lazy dog",
"The quick brown fox jumps over the lazy dog",
"\n"),
(0, "The quick brown fox jumps over the lazy dog".to_string()));
}