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: Wrong Answer)

#include <cstdio>
#include <algorithm>

using namespace std;

const int N=1e5+10;

struct Edge{ int to,next; };

Edge way[N<<1];
int n,tot,num[N],l[N],r[N],col[N],k;

void Build(int a,int b) { way[++tot]=(Edge){b,num[a]}; num[a]=tot; }

void Init()
{
	scanf("%d",&n);
	int a,b;
	for (int i=2;i<=n;++i)
	{
		scanf("%d%d",&a,&b);
		Build(a,b);
		Build(b,a);
	}
	for (int i=1;i<=n;++i) l[i]=-1e6,r[i]=1e6;
	scanf("%d",&k);
	for (int i=1;i<=k;++i)
	{
		scanf("%d%d",&a,&b);
		l[a]=r[a]=b;col[a]=b;
	}
}

void Dfs(int x,int fa)
{
	for (int i=num[x];i;i=way[i].next)
	{
		int v=way[i].to;
		if (v==fa) continue;
		l[v]=max(l[x]-1,l[v]);
		r[v]=min(r[x]+1,r[v]);
		Dfs(v,x);
	}
	if (fa)
	{
		l[fa]=max(l[x]-1,l[fa]);
		r[fa]=min(r[x]+1,r[fa]);
	}
}

bool use[2000010];
bool FindAns(int x,int fa)
{
	for (int i=num[x];i;i=way[i].next)
	{
		int v=way[i].to;
		if (l[v]==r[v]) use[l[v]+1000000]=1;
	}
	col[x]=l[x];
	while (use[col[x]+1000000] && col[x]<=r[x]) col[x]++;
	if (col[x]>r[x]) { col[x]=-1e7; return 0; }
	
	l[x]=r[x]=col[x];
	for (int i=num[x];i;i=way[i].next)
	{
		int v=way[i].to;
		if (l[v]==r[v]) use[l[v]+1000000]=0;
	}
	
	for (int i=num[x];i;i=way[i].next)
	{
		int v=way[i].to;
		if (v==fa) continue;
		l[v]=max(l[x]-1,l[v]);
		r[v]=min(r[x]+1,r[v]);
		if (!FindAns(v,x)) return 0;
	}
	return 1;
	
}

void Solve()
{
	Dfs(1,0);
	
	bool fl=1;
	for (int i=1;i<=n;++i) if (l[i]>r[i]) fl=0;
	
	if (!fl) printf("No\n");
	else 
	{
		fl=FindAns(1,0);
		if (!fl) { printf("No\n"); return ; }
		printf("Yes\n");
		for (int i=1;i<=n;++i) printf("%d\n",col[i]);
	}
	
}

int main()
{
	Init();
	Solve();
	return 0;
}