Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Problem E
Black or White

Here lies a row of a number of bricks each painted either black or white. With a single stroke of your brush, you can overpaint a part of the row of bricks at once with either black or white paint. Using white paint, all the black bricks in the painted part become white while originally white bricks remain white; with black paint, white bricks become black and black ones remain black. The number of bricks painted in one stroke, however, is limited because your brush cannot hold too much paint at a time. For each brush stroke, you can paint any part of the row with any number of bricks up to the limit.

In the first case of the sample input, the initial colors of four bricks are black, white, white, and black. You can repaint them to white, black, black, and white with two strokes: the first stroke paints all four bricks white and the second stroke paints two bricks in the middle black.

Your task is to calculate the minimum number of brush strokes needed to change the brick colors as specified. Never mind the cost of the paints.

Input

The input consists of a single test case formatted as follows.

$n$ $k$
$s$
$t$

The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 500 000$). $n$ is the number of bricks in the row and $k$ is the maximum number of bricks painted in a single stroke. The second line contains a string $s$ of $n$ characters, which indicates the initial colors of the bricks. The third line contains another string $t$ of $n$ characters, which indicates the desired colors of the bricks. All the characters in both s and t are either B or W meaning black and white, respectively.

Output

Output the minimum number of brush strokes required to repaint the bricks into the desired colors.

Sample Input 1

4 4
BWWB
WBBW

Sample Output 1

2

Sample Input 2

4 3
BWWB
WBBW

Sample Output 2

3

Sample Input 3

4 3
BWWW
BWWW

Sample Output 3

0

Sample Input 4

7 1
BBWBWBW
WBBWWBB

Sample Output 4

4

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
using namespace std;
 
template <typename T, class Compare = less<T> >
struct sliding_window {
    deque<pair<int, T> > data;
    function<bool (T const &, T const &)> compare;
    sliding_window(Compare const & a_compare = Compare()) : compare(a_compare) {}
    T front() { return data.front().second; }  // O(1), minimum
    void push_back(int i, T a) { while (not data.empty() and compare(a, data.back().second)) data.pop_back(); data.emplace_back(i, a); }  // O(1) amortized.
    void pop_front(int i) { if (data.front().first == i) data.pop_front(); }
    void push_front(int i, T a) { if (data.empty() or not compare(data.front().second, a)) data.emplace_front(i, a); }
};
 
constexpr int inf = 1e9 + 7;
int main() {
    // input
    int n, k; cin >> n >> k;
    string s, t; cin >> s >> t;
    // solve
    vector<int> delta(n + 1);
    REP (i, n) {
        delta[i + 1] = delta[i] + (i == 0 or t[i - 1] != t[i]);
    }
    vector<int> dp(n + 1, inf);
    sliding_window<int> sw_b, sw_w;
    dp[0] = 0;
    sw_b.push_back(0, 2 * dp[0] - delta[0] + (t[0] == 'B'));
    sw_w.push_back(0, 2 * dp[0] - delta[0] + (t[0] == 'W'));
    REP3 (r, 1, n + 1) {
        if (r - k - 1 >= 0) {
            sw_b.pop_front(r - k - 1);
            sw_w.pop_front(r - k - 1);
        }
        dp[r] = (s[r - 1] == t[r - 1]) ?
            dp[r - 1] :
            ((t[r - 1] == 'W' ? sw_b : sw_w).front() + delta[r]) / 2 + 1;
        if (r < n) {
            sw_b.push_back(r, 2 * dp[r] - delta[r] + (t[r] == 'B'));
            sw_w.push_back(r, 2 * dp[r] - delta[r] + (t[r] == 'W'));
        }
    }
    // output
    cout << dp[n] << endl;
    return 0;
}