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 Java7 (OpenJDK 1.7.0) (judge verdict: Runtime Error)

import java.io.*;
import java.lang.*;
import java.util.*;

public class Main {

	static boolean debug = false;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Tree tree = new Tree(N);
		for (int j = 0; j < N - 1; j++) {
			int x = sc.nextInt(), y = sc.nextInt();
			tree.insert(x - 1, y - 1);
			tree.insert(y - 1, x - 1);
		}
		int K = sc.nextInt(), start = 0;
		Color[] color = new Color[N];
		for (int j = 0; j < K; j++) {
			int v = sc.nextInt();
			color[v - 1] = new Color(sc.nextInt(), 1);
			start = v - 1;
		}
		if (solve(tree, color, -1, start)) {
			System.out.println("Yes");
			for (int j = 0; j < N; j++)
				System.out.println(color[j].start);
		}
		else
			System.out.println("No");
	}

	private static boolean solve(Tree tree, Color[] color, int parent, int node) {
		if (debug) System.out.println(String.format("solve(tree, color, %d, %d)", parent, node));
		if (color[node] == null)
			color[node] = color[parent].expand();
		Node child = tree.first(node);
		while (child != null) {
			if (child.node != parent) {
				if (!solve(tree, color, node, child.node))
					return false;
				color[node] = merge(color[node], color[child.node]);
				if (color[node] == null)
					return false;
			}
			child = tree.next(child);
		}
		return true;
	}

	private static Color merge(Color current, Color child) {
		if (debug) System.out.println(String.format("merge(current: [%d, %d] with child: [%d, %d])",
			current.start, current.count, child.start, child.count));
		Color merge;
		if (current.start % 2 == child.start % 2)
			merge = null;
		Color expand = child.expand();
		long start = Math.max(expand.start, current.start);
		long end = Math.min(current.start + 2 * (current.count - 1), expand.start + 2 * (expand.count - 1));
		merge = new Color(start, (int) (end - start + 2) / 2);
		if (merge.count <= 0)
			merge = null;
		if (debug) System.out.println(merge != null ? String.format("merge result: [%d, %d]", merge.start, merge.count) : "merge result: [null]");
		return merge;
	}

	static class Color {
		long start;
		int count;

		public Color(long start, int count) {
			this.start = start;
			this.count = count;
		}

		public Color expand() {
			 return new Color(start - 1, count + 1);
		}
	}

	static class Tree {
		
		private Node[] tree;
		private int[] last;
		private int current;

		public Tree(int N) {
			tree = new Node[2 * N + 1];
			last = new int[N];
			current = 1;
		}

		public Node next(Node node) {
			return node.next > 0
				? tree[node.next]
				: null;
		}
		public Node first(int node) {
			return tree[last[node]];
		}

		public void insert(int x, int y) {
			tree[current] = new Node(x, last[y]);
			last[y] = current++;
		}

	}
	static class Node {
		int node, next;

		public Node(int node, int next) {
			this.node = node;
			this.next = next;
		}
	}

}