Problem:
Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9

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

#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
#include <deque>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SIZE = 1e6 + 10, S2 = 1e7 * 2, MOD = 1e9 + 7, INF = 1e9 * 1e9;

vector<vector<ll>> graph;
ll col[SIZE], dist[SIZE], par[SIZE];
pair<ll, ll> bst[SIZE];
bool used[SIZE];

pair<ll, ll> dfs(int v, int par = -1) {
    used[v] = 1;

    /*if (col[v] != -1) {
        if (abs(col[par] - col[v]) % 2 != abs(dist[par] - dist[v]) % 2) {
            cout << "No";
            exit(0);
        }
    }*/
    pair<ll, ll> pr = { -INF, INF };
    if (par != -1) pr = { col[par] - 1, col[par] + 1 };
    if (col[v] != -1) {
        pr = { col[v], col[v] };
    }
    for (auto to : graph[v]) {
        if (!used[to]) {
            dist[to] = dist[v] + 1;
            if (col[v] == -1) {
                pair<ll, ll> q = dfs(to, par);
                pr.first = max(pr.first, q.first - 1);
                pr.second = min(pr.second, q.second + 1);
            }
            else {
                pair<ll, ll> q = dfs(to, v);
                pr.first = max(pr.first, q.first - 1);
                pr.second = min(pr.second, q.second + 1);
            }
        }
    }

    bst[v] = pr;
    return pr;
}

void ch(int v, int par = -1) {
    used[v] = 1;

    if (col[v] != -1 && !(bst[v].first <= col[v] && bst[v].second >= col[v])) {
        cout << "No";
        exit(0);
    }

    bool flag = true;
    if (col[v] == -1) {
        if (bst[v].first <= col[par] - 1 && bst[v].second >= col[par] - 1) {
            col[v] = col[par] - 1;
            flag = false;
        }
        if (flag && bst[v].first <= col[par] + 1 && bst[v].second >= col[par] + 1) {
            flag = false;
            col[v] = col[par] + 1;
        }
    }
    else {
        flag = false;
    }

    if (flag) {
        cout << "No";
        exit(0);
    }
    for (auto to : graph[v]) {
        if (!used[to]) {
            ch(to, v);
        }
    }
}

int main() {
    fastInp;

    string s;
    ll cnt[2];
    cnt[0] = 0;
    cnt[1] = 0;

    cin >> s;

    ll pr = -1;
    for (int i = 0; i < s.size(); i++) {
        ll cr = 0;
        if (s[i] == 'W') cr = 1;

        if (cr != pr) {
            cnt[pr]++;
            pr = cr;
        }
    }

    cnt[pr]++;

    cout << cnt[0] + cnt[1] - 1;
    return 0;
}