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


    给出两个长度分别为n1,n2(n1,n2≤100)且每列高度只为1或2的长条。需要将它们放入一个高度为3的容器(如图3-8所示),问能够容纳它们的最短容器长度。
    在这里插入图片描述

    样例

    2112112112
    2212112
    10
    
    12121212
    21212121
    8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    06 在MSYS2中编译树莓派裸机程序,并在QEMU中运行
    MySQL SQL语法基础
    一文读懂 HTTP/2 特性
    .NET7 for LoongArch64(国产龙芯)
    c语言 二分查找(迭代与递归)
    基于yoloV7添加关键点训练记录
    想学硬件,该学什么啊?
    1.【算法五章】
    22_ue4进阶末日生存游戏开发[EQS]
    数值去0操作
  • 原文地址:https://blog.csdn.net/inxunxun/article/details/133856725