给出两个长度分别为n1,n2(n1,n2≤100)且每列高度只为1或2的长条。需要将它们放入一个高度为3的容器(如图3-8所示),问能够容纳它们的最短容器长度。
样例
2112112112
2212112
10
12121212
21212121
8
rust
use std::io;
fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let v1: Vec<_> = buf
.trim()
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect();
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();
let v2: Vec<_> = buf
.trim()
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect();
//println!("{:?}", v1);
//println!("{:?}", v2);
let mut len = calclen(&v1, &v2);
len = len.min(calclen(&v2, &v1));
println!("{}", len);
}
fn calclen(v1: &Vec<u32>, v2: &Vec<u32>) -> usize{
let mut minlen = usize::MAX;
let mut startidx = 0;
//固定v1,向右移动v2
'foo: while startidx <= v1.len() {
startidx += 1;
let startidx = startidx - 1;
let mut container = vec![0; v2.len() + v1.len()];
for idx in 0..container.len() {
if idx < v1.len() {
container[idx] += v1[idx];
}
if idx >= startidx && idx - startidx < v2.len() {
container[idx] += v2[idx - startidx];
}
if container[idx] > 3 {
continue 'foo;
}
}
loop {
if let Some(0) = container.last() {
container.pop();
} else {
break;
}
}
if container.len() < minlen{
minlen = container.len();
}
//println!("{:?} {}", container, container.len());
}
return minlen;
}