AtCoder Beginner Contest 258 C

https://atcoder.jp/contests/abc258/tasks/abc258_c

1の処理をする代わりに先頭の位置を変えます。

// Rotation
#![allow(non_snake_case)]

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 main() {
    let w = read_vec();
    let N: usize = w[0];
    let Q: usize = w[1];
    let S: String = read();
    let cs: Vec<char> = S.chars().collect();
    let queries: Vec<(usize, usize)> = (0..Q).map(|_| read_vec()).
                                            map(|v| (v[0], v[1])).collect();
    let mut pos = 0;
    for (t, x) in queries.iter() {
        match t {
            1 => { pos = (pos - x + N) % N },
            2 => { println!("{}", cs[(pos + x - 1)%N]) },
            _ => ()
        }
    }
}