Problem:
Score : 900 points

Problem Statement

Takahashi has a lot of peculiar devices.
These cylindrical devices receive balls from left and right.
Each device is in one of the two states A and B, and for each state, the device operates as follows:

When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.

When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.

The transition of the state of a device happens momentarily and always completes before it receives another ball.

Takahashi built a contraption by concatenating N of these devices. In this contraption,

A ball that was thrown out from the right side of the i-th device from the left (1 \leq i \leq N-1) immediately enters the (i+1)-th device from the left side.

A ball that was thrown out from the left side of the i-th device from the left (2 \leq i \leq N) immediately enters the (i-1)-th device from the right side.

The initial state of the i-th device from the left is represented by the i-th character in a string S.
From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end.
Here, it can be proved that the ball always comes out of the contraption after a finite time.
Find the state of each device after K balls are processed.

Constraints

1 \leq N \leq 200,000

1 \leq K \leq 10^9

|S|=N

Each character in S is either A or B.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print a string that represents the state of each device after K balls are processed.
The string must be N characters long, and the i-th character must correspond to the state of the i-th device from the left.

Sample Input 1

5 1
ABAAA

Sample Output 1

BBAAA

In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.

Sample Input 2

5 2
ABAAA

Sample Output 2

ABBBA

Sample Input 3

4 123456789
AABB

Sample Output 3

BABA

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;
#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}
fn get_word() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

fn fool_one(s: &mut [char]) -> bool {
    let mut pos = 0;
    let mut dir = true;
    let n = s.len();
    loop {
        if (!dir && pos == 0) || (dir && pos >= n) {
            return dir;
        }
        let c = if dir { &mut s[pos] } else { &mut s[pos - 1] };
        let is_a = *c == 'A';
        *c = if is_a { 'B' } else { 'A' };
        if is_a { // reflect
            dir = !dir;
        } else {
            pos = if dir { pos + 1 } else { pos - 1 };
        }
    }
}

fn norm(seq: Vec<(usize, bool)>) -> Vec<(usize, bool)> {
    let mut cnt: usize = 0;
    let mut cur = false;
    let mut ret = Vec::new();
    for v in seq {
        if v.1 != cur {
            if cnt > 0 {
                ret.push((cnt, cur));
            }
            cur = v.1;
            cnt = 0;
        }
        cnt += v.0;
    }
    if cnt > 0 {
        ret.push((cnt, cur));
    }
    while ret.len() >= 2 {
        let k = ret.len();
        if ret[k - 2].0 == 1 && ret[k - 1].0 == 1 && !ret[k - 1].1 {
            ret.pop();
            ret.pop();
        } else {
            break;
        }
    }
    ret
}

// B: true, A: false
fn smart_one(seq: &[(usize, bool)]) -> Vec<(usize, bool)> {
    let mut ret = Vec::new();
    let m = seq.len();
    for i in 0 .. m {
        if seq[i].1 { // B
            if i < m - 1 {
                if seq[i].0 >= 2 {
                    ret.push((seq[i].0 - 1, false)); // A
                }
                ret.push((1, true)); // B
            } else {
                ret.push((seq[i].0, false));
            }
        } else {
            if i == 0 {
                ret.push((1, true));
                if seq[0].0 >= 2 {
                    ret.push((seq[0].0 - 1, false));
                }
                for j in 1 .. m {
                    ret.push(seq[j]);
                }
                return norm(ret);
            }
            // [i - 1] should be B.
            if seq[i].0 >= 2 {
                ret.push((seq[i].0 - 1, true)); // B
            }
            ret.push((1, false));
        }
    }
    norm(ret)
}

fn test(s: &[char]) {
    let mut s = s.to_vec();
    for _ in 0 .. 64 {
        let r = fool_one(&mut s);
        println!("{} {} {}", if r { " " } else { "+" },
                 s.iter().map(|&x| if x == 'A' { 'A' } else {'.'}).collect::<String>(),
                 if r { "+" } else { " " });
    }
}

fn solve() {
    let n: usize = get();
    let k: i64 = get();
    let s: Vec<char> = get_word().chars().collect();
    let mut seq = Vec::new();
    let mut c = 0;
    let mut cur = ' ';
    for &v in s.iter() {
        if cur != v {
            if c > 0 {
                seq.push((c, cur == 'B'));
            }
            c = 0;
            cur = v;
        }
        c += 1;
    }
    if c > 0 {
        seq.push((c, cur == 'B'));
    }
    for _ in 0 .. k {
        seq = smart_one(&seq);
        if seq.len() == 0 {
            break;
        }
    }
    // recons
    let mut out = String::new();
    let mut l = 0;
    for (v, c) in seq {
        for _ in 0 .. v {
            out.push(if c { 'B' } else { 'A' });
        }
        l += v;
    }
    for _ in 0 .. (n - l) / 2 {
        out.push('B');
        out.push('A');
    }
    println!("{}", out);
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}