AtCoder Beginner Contest 257 B

https://atcoder.jp/contests/abc257/tasks/abc257_b

文字列のjoinは普通にできるので、ToString traitを持つ型のVecを引数にする関数を作ってみました。

fn join<T: ToString>(v: &Vec<T>) -> String {
    v.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(" ")
}

mutableなメソッドはこんな感じで書くんですね。

    fn move_piece(&mut self, L: usize) {
        ...
    }
// 1D Pawn
#![allow(non_snake_case)]


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}

fn join<T: ToString>(v: &Vec<T>) -> String {
    v.iter().map(|x| x.to_string()).collect::<Vec<String>>().join(" ")
}


//////////////////// Squares ////////////////////

struct Squares {
    N: usize,
    v: Vec<usize>
}

impl Squares {
    fn move_piece(&mut self, L: usize) {
        if L == self.v.len() {
            if self.v[L-1] < self.N {
                self.v[L-1] += 1
            }
        }
        else {
            if self.v[L-1] < self.v[L] - 1 {
                self.v[L-1] += 1
            }
        }
    }
    
    fn print(&self) {
        println!("{}", join(&self.v))
    }
    
    fn create(N: usize, A: &Vec<usize>) -> Squares {
        Squares { N, v: A.clone() }
    }
}


//////////////////// process ////////////////////

fn main() {
    let v = read_vec();
    let N: usize = v[0];
    let _K = v[1];
    let _Q = v[2];
    let A = read_vec();
    let L = read_vec();
    let mut sq = Squares::create(N, &A);
    for &l in L.iter() {
        sq.move_piece(l)
    }
    sq.print()
}