换低挡装置(Kickdown, ACM/ICPC NEERC 2006, UVa1588)rust解法

给出两个长度分别为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;
}
相关推荐
Victor35634 分钟前
MySQL(138)如何设置数据归档策略?
后端
Victor35635 分钟前
MySQL(137)如何进行数据库审计?
后端
Sylvia-girl5 小时前
Java——抽象类
java·开发语言
Yana.nice7 小时前
Bash函数详解
开发语言·chrome·bash
FreeBuf_8 小时前
黄金旋律IAB组织利用暴露的ASP.NET机器密钥实施未授权访问
网络·后端·asp.net
tomorrow.hello9 小时前
Java并发测试工具
java·开发语言·测试工具
晓13139 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊9 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
张小洛9 小时前
Spring AOP 是如何生效的(入口源码级解析)?
java·后端·spring
我是前端小学生10 小时前
Rust中的Vec数据结构介绍
rust