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



Sample input:
BBBWW


Sample output:
1

Submission status: Runtime Error

Solution code:
w, h, n = map(int,input().split())

y_min = 0
y_max = h

x_min = 0
x_max = w

for i in range(n):
  x, y, a = map(int,input().split())

  if a == 1:
    x_min = max(x_min, x)

  if a == 2:
    x_max = min(x_max, x)

  if a == 3:
    y_min = max(y_min, y)

  if a == 4:
    y_max = min(y_max, y)

white = (y_max - y_min) * (x_max - x_min)
if (y_max - y_min)>=0 and (x_max - x_min)>=0:
  print(max(white, 0))
else:
  print(0)

Submission status: Runtime Error

Solution code:
w, h, n = map(int,input().split())

#とりうるx,yの範囲
y_min = 0
y_max = h

x_min = 0
x_max = w

for i in range(n):
  x, y, a = map(int,input().split())

  if a == 1:
    x_min = max(x_min, x)

  if a == 2:
    x_max = min(x_max, x)

  if a == 3:
    y_min = max(y_min, y)

  if a == 4:
    y_max = min(y_max, y)

white = (y_max - y_min) * (x_max - x_min)
if (y_max - y_min)>=0 and (x_max - x_min)>=0:
  print(max(white, 0))
else:
  print(0)

Submission status: Runtime Error

Solution code:
def main():
    s = input()
    slen = len(s)
    cnt = 0
    last = 0
    if s[0] == "W":
        for i in range(slen-1):
            if s[i] == "W" and s[i+1] =="B":
                cnt += 1
        if s[-1] == "W" and s[-2] == "B":
            last = 1
    if s[0] == "B":
        for i in range(slen-1):
            if s[i] == "B" and s[i+1] =="W":
                cnt += 1
        if s[-1] == "B" and s[-2] == "W":
            last = 1        
    if cnt == 0:
        print(cnt)
    else:
        print(cnt*2 - 1 + last)
    

 
    
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
s=input()
now,ans=0,0
while now<n-1:
    if s[now]!=s[now+1]:
        ans+=1
    now+=1
print(ans)

Submission status: Runtime Error

Solution code:
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    char[] s = sc.next().toCharArray();
    sc.close();
    int ans = 0;
    char b = s[0];
    for (char i:s){
      if (b != i){
        ans++;
        b = i;
      }
    }
    System.out.println(ans);
  }
}

Submission status: Runtime Error

Solution code:
string = input()
ans = 0
for i in range(len(string)-1):
    if string[i] != string[i+1]:
        ans += 1
if string[-1] != string[-2]:
    ans += 1
if ans > 0:
    print(ans-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
if len(list(set(S))) != 0:
    i =0
    count = 0
    while True:
        if S[i] != S[i+1]:
            count += 1
            S[i] = S[i + 1]
        i += 1
        if i == (len(S)-1):
            break
else:
    count = 0
print(count)

Submission status: Runtime Error

Solution code:
S = list(input())
if len(list(set(S))) != 0:
    i =0
    count = 0
    while True:
        if S[i] != S[i+1]:
            count += 1
            S[i] = S[i + 1]
        i += 1
        if i == (len(S)-1):
            break
print(count)

Submission status: Runtime Error

Solution code:
s=input()
from itertools import groupby as gb
print(len(gb(s)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)

S = list(input())
L = len(S)
b = S.count("B")
w = S.count("W")

def f(i, result):
  if S[i] == S[i-1]:
    if i == 1:
      print(result)
      exit()
    else:
      f(i-1, result)
  else:
    if i == 1:
      result += 1
      print(result)
      exit()
    else:
      result += 1
      f(i-1, result)
      
if L == 1:
  result = 0
  print(result)
  exit()
elif b == L or w == L:
  result = 0
  print(result)
  exit()
else:
  f(L-1, 0)

Submission status: Runtime Error

Solution code:
S = list(input())
L = len(S)
b = S.count("B")
w = S.count("W")

def f(i, result):
  if S[i] == S[i-1]:
    if i == 1:
      print(result)
      exit()
    else:
      f(i-1, result)
  else:
    if i == 1:
      result += 1
      print(result)
      exit()
    else:
      result += 1
      f(i-1, result)
      
if L == 1:
  result = 0
  print(result)
  exit()
elif b == L or w == L:
  result = 0
  print(result)
  exit()
else:
  f(L-1, 0)

Submission status: Runtime Error

Solution code:
S = list(input())
L = len(S)

def f(i, result):
  if S[i] == S[i-1]:
    if i == 1:
      print(result)
      exit()
    else:
      f(i-1, result)
  else:
    if i == 1:
      result += 1
      print(result)
      exit()
    else:
      result += 1
      f(i-1, result)
      
if L == 1:
  result = 0
  print(result)
  exit()
else:
  f(L-1, 0)

Submission status: Runtime Error

Solution code:
S = input()

length = len(S)
ans = 0

for i in range(n-1):
  if S[i] != S[i+1]:
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
S=input()
for i in range(len(S)-1):
  cnt=0
  if S[i]!=S[i+1]:
    cnt+=1
print(cnt)

Submission status: Runtime Error

Solution code:
S = input()
w = S.split('B')
w.remove('')
b = S.split('W')
b.remove('')

print(len(w)+len(b)-1)

Submission status: Runtime Error

Solution code:
s=input()
ans=[]
for i in range(len(s)-1):
	if s[i] != s[i+1]:
		ans.append(s[i])
if s[-1] != s[-2]:
	ans.append(s[-1])
print(ans)
print(len(ans)-1)

Submission status: Runtime Error

Solution code:
a=input()
cnt=0
for i in range(len(a)-1):
  if(a[i]==a[i+1]):
    cnt+=1
print(cmt)

Submission status: Runtime Error

Solution code:
word=inpput()
# word="WBWBWBWBWB"
ans=0
for i in range(len(word)-1):
    if word[i]!=word[i+1]:
        ans+=1
        
print(ans)

Submission status: Runtime Error

Solution code:
S = raw_input()

def count_least_turns(s, n=0):
    stripped = s.lstrip(s[0])
    if len(stripped):
        return count_least_turns(stripped, n + 1)
    return n

print count_least_turns(S)

Submission status: Runtime Error

Solution code:
S = raw_input()

def count_least_turns(s, n=0):
    stripped = s.lstrip(s[0])
    if len(stripped) <= 1:
        return n
    return count_least_turns(stripped, n + 1)

print count_least_turns(S)

Submission status: Runtime Error

Solution code:
S = raw_input()

def count_least_turns(s, n=0):
    print s
    stripped = s.lstrip(s[0])
    if len(stripped) <= 1:
        return n
    return count_least_turns(stripped, n + 1)

print count_least_turns(S)

Submission status: Runtime Error

Solution code:
import re
# 盤面の状態
s = input()

cnt = 0

while True:
    print(s)
    
    # 全部Wになるまで or 1文字残しまで繰り返す
    if s.count("W") == len(s):
        break

    # Bがどちらにあるか調べる
    if s[0] != "B":
        # 逆向きにする
        s = s[::-1]
        
    # 先頭を置換する
    if s[0] == "B":
        s = re.sub(r'[B]+', "", s,1)
        cnt +=1
    elif s[0] == "W":
        s = re.sub(r'[W]+', "", s,1)
        cnt +=1

print(cnt)

Submission status: Runtime Error

Solution code:
import re
# 盤面の状態
s = input()

cnt = 0

while True:
    print(s)
    
    # 全部Wになるまで or 1文字残しまで繰り返す
    if s.count("W") == len(s):
        break

    # Bがどちらにあるか調べる
    if s[len(s)-1] == "B":
        # 逆向きにする
        s = s[::-1]
        
    # 先頭を置換する
    if s[0] == "B":
        s = re.sub(r'[B]+', "", s,1)
        cnt +=1
    elif s[0] == "W":
        s = re.sub(r'[W]+', "", s,1)
        cnt +=1

print(cnt)

Submission status: Runtime Error

Solution code:
import re
# 盤面の状態
s = input()

cnt = 0

while True:
    print(s)
    
    # 全部Wになるまで or 1文字残しまで繰り返す
    if s.count("W") == len(s):
        break

    # Bがどちらにあるか調べる
    if s[len(s)-1] == "B":
        # 逆向きにする
        s = s[::-1]
    else:
        # Bが末尾に見つからなければ、何もしない
        pass
        
    # 先頭を置換する
    if s[0] == "B":
        s = re.sub(r'[B]+', "", s,1)
        cnt +=1
    elif s[0] == "W":
        s = re.sub(r'[W]+', "", s,1)
        cnt +=1

print(cnt)

Submission status: Runtime Error

Solution code:
import re
# 盤面の状態
s = input()

cnt = 0

while True:
    # 全部Wになるまで or 1文字残しまで繰り返す
    if s.count("W") == len(s) or len(s)==1:
        break
    else:
        print(s)

    # 先頭を置換する
    if s[0] == "W":
        s = re.sub(r'[W]+', "", s,1)
        cnt +=1
    elif s[0] == "B":
        s = re.sub(r'[B]+', "", s,1)
        cnt +=1

print(cnt)

Submission status: Runtime Error

Solution code:
s = input()
f = 0
ans = 0
if s[0] == "B":
    ans += 1
    while s[0] == "B":
        s = s[1:]

s = s[::-1]

if s[0] == "B":
    ans += 1
    while s[0] == "B":
        s = s[1:]

print(ans+s.count("B")*2)

Submission status: Runtime Error

Solution code:
s = input()
w = ""
cnt1 = 0
for x in s[1:]:
	if x != w:
      cnt1 += 1

cnt2 = 0
for x in s[-2::-1]:
	if x != w:
      cnt2 += 1

print(min(cnt1, cnt2))

Submission status: Runtime Error

Solution code:
ans = 0
S = input()
for i in range(1,len(S)): if S[i-1] != S[i]: ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())
i = 0
count = 0
while True:
    #print("line6 s=",s,"i =",i,"s[i] =",s[i])
    if len(s)==1:break
    if len(set(s)) == 1:break
    while s[i] == s[i+1]and i<len(s)-2:
        i+=1
    s[i] = s[i+1]
    count += 1
    j=0
    while j<=i:
        s[j]=tmp
        j+=1

print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
i = 0
count = 0
while True:
    print("line6 s=",s,"i =",i,"s[i] =",s[i])
    if len(s)==1:break
    if len(set(s)) == 1:break
    while s[i] == s[i+1]and i<len(s)-2:
        i+=1
    s[i] = s[i+1]
    count += 1
    j=0
    while j<=i:
        s[j]=tmp
        j+=1

print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
i = 0
count = 0
while True:
    #print("line6 s=",s,"i =",i,"s[i] =",s[i])
    while s[i] == s[i+1]and i<len(s)-2:
        i+=1
    if len(set(s)) == 1:
        break
    tmp = s[i+1]
    count += 1
    j=0
    while j<=i:
        s[j]=tmp
        j+=1

print(count)

Submission status: Runtime Error

Solution code:
s = input()

left = 0
right = 0

count = 0
while True:
  if s[right+1] != s[left]:
    count += 1
    right += 1
    left = right
    if right == len(s) - 1:
      break
  else:
    right += 1
    if right == len(s) - 1:
      break

print(count)

Submission status: Runtime Error

Solution code:
b = input()
d = []
isWhite = False
cnt = 0
for i in range(len(b-1)):
  if b[i] != b[i+1]:
    d.append(cnt)
  else:
    cnt+=1

print(len(d))

Submission status: Runtime Error

Solution code:
S = input()
count = 0
temp = S[-1]
for s in S[-2::-1]:
    if s != temp:
        count += 1
    temp = s
print(0 if count == 0 else temp-1)

Submission status: Runtime Error

Solution code:
def _1d_reversi(S: str) -> int:
    n = len(S)
    return sum(s[i] != s[i-1] for i in range(1, n))


if __name__ == "__main__":
    S = input()
    ans = _1d_reversi(S)
    print(ans)

Submission status: Runtime Error

Solution code:
s=int(input())
cnt=0
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    cnt+=1
print(cnt)

Submission status: Runtime Error

Solution code:
s=input()
ans=0
for i in range(len(s):):
  if s[i]!=s[i+1]:
    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
ans = 0
for i in range(1,n):
    if(s[i]!=s[i-1]):
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
[print(len(list(i.groupby(input())))-1)for i in[__import__('itertools')]][print(len(list(i.groupby(input())))-1)for i in[__import__('itertools')]]

Submission status: Runtime Error

Solution code:
s=str(input())
ans=0
for i in range(len(s)-1):
  if s[i]<>[i+1]:
    ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
ans = 0
for i in range(len(s-1)):
  if s[i] != s[i+1]:
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
n = len(s)
ct = 0
if len(set(s)) == 1:
  ans = 0
else:
 for i in range(n-1):
  if s[i] != s[i+1]:
    ans+= 1
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
s = s[0]
ans = 0
for i in S:
    if s != i:
        ans += 1
        s = i
print(ans)

Submission status: Runtime Error

Solution code:
S = input()

s = ''
for i in S:
    if i != s[-1]:
        s += i

print(len(s) - 1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8

import sys


def leftside_reverse(s, turn):
    for num in range(len(s)):
        if s[num] == turn:
            break
        else:
            s[num] = turn

    return s


def rightside_reverse(s, turn):
    for num in range(len(s) - 1, -1):
        if s[num] == turn:
            break
        else:
            s[num] = turn

    return s


def function(s, first):
    count = 0
    turn = first

    while True:

        if s.count(first) == len(s):
            break

        if s[0] == turn:
            s = rightside_reverse(s, turn)
            count += 1
        else:
            s = leftside_reverse(s, turn)
            count += 1

        if turn == 'B':
            turn = 'W'
        else:
            turn = 'B'

    return count


if __name__ == '__main__':

    s = list(input())
    b = 'B'
    w = 'W'

    if s.count(b) == len(s) or s.count(w) == len(s):
        print('0')
        sys.exit(1)

    b_count = function(s, b)
    w_count = function(s, w)

    print(min(b_count, w_count))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8

import sys


def leftside_reverse(s, turn):
    for num in len(s):
        if s[num] == turn:
            return s
        else:
            s[num] = turn

    return s


def rightside_reverse(s, turn):
    for num in range(len(s), 0, -1):
        if s[num] == turn:
            return s
        else:
            s[num] = turn

    return s


def function(s, turn):
    count = 0

    while True:
        if s.count(turn) == len(s):
            return count

        if s[0] == turn:
            s = leftside_reverse(s, turn)
            count += 1
        else:
            s = rightside_reverse(s, turn)
            count += 1

        if turn == b:
            turn = w
        else:
            turn = b


if __name__ == '__main__':

    s = list(input())
    b = 'B'
    w = 'W'

    if s.count(b) == len(s) or s.count(w) == len(s):
        print('0')
        sys.exit(1)

    b_count = function(s, b)
    w_count = function(s, w)

    print(min(b_count, w_count))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
int main(void){
  string S;
  cin>>S;
  int ans=0, i, tmp=S[0];
  for (i=0;i<S.length();i++){
    if (S[i]!=tmp){
      ans+=1;
      tmp=S[i];
    }
  }
  cout<<ans;
}

Submission status: Runtime Error

Solution code:
s=input()
a=0
for i in range(1,n):
    if s[i]!=s[i-1]:
        a+=1
print(a)

Submission status: Runtime Error

Solution code:
s, ans = raw_input(), 0
for i in range(len(s) - 1):
    if (len(set(s[i : i + 2))) == 2):
        ans += 1
print ans

Submission status: Runtime Error

Solution code:
s, ans = raw_input(), 0
for i in range(len(s) - 1):
    if (len(set(s[i : i + 2))) == 2):
        ans += 1
print cnt

Submission status: Runtime Error

Solution code:
s, ans = raw_input(), 0
for i in range(len(s) - 1):
    if (len(set(s[i : i + 2))) == 2):
        cnt += 1
print cnt

Submission status: Runtime Error

Solution code:
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(1, n-1, 2):
        if a[i+1] > a[i-1]:
            if a[i+1] + a[i] < x:
                continue
            ans += (a[i+1] + a[i]) -x
            if a[i+1] > x:
                a[i+1] = x
        else:
            if a[i-1] + a[i] < x:
                continue
            ans += (a[i-1] + a[i]) -x
    if len(a) % 2 == 0:
        if a[-1] + a[-2] < x:
            return ans
        ans += (a[-1] + a[-2]) -x
        return ans
    else:
        return ans

print(m())

Submission status: Runtime Error

Solution code:
#D    高橋君と見えざる手 / An Invisible Hand
N,T = [int(i) for i in input().split(" ")]
A=[int(i) for i in input().split(" ")]
Max = 0
count = 1
for i in range(N-1,0,-1):
    for j in range(i-1,-1,-1):
        temp = A[i]-A[j]
        if temp>Max:
            Max = temp
            count = 1
        elif temp==Max:
            count +=1

print(count)

Submission status: Runtime Error

Solution code:
import re
print(len(re.sub("B+","B",re.sub("W+","W",S)))-1)

Submission status: Runtime Error

Solution code:
# coding: utf-8
 
# R: Right hand side
# L: Left  hand side
def add_stone(stone, pos, searched_stones):
    sample = searched_stones[::[1, -1][pos == "R"]]
    if sample[0] == stone:
        return None
 
    for n, s in enumerate(sample[1:]):
        if s == stone:
            return stone * (n + 2) + sample[1 + n:]
 
 
def stones_bfs(searched_stones):
    
    queue = [searched_stones + "0"]
    patterns = [["B", "R"], ["B", "L"], ["W", "R"], ["W", "L"]]
    
    while queue:
        stones = queue.pop(0)
        if (stones.count("W") == len(stones) - 1 |
            stones.count("B") == len(stones) - 1):
            return int(stones[-1])
            
        for p in patterns:
            s = add_stone(*p, stones[:-1])
            if s is not None:
                queue += [s + str(int(stones[-1]) + 1)]
 
print(stones_bfs(input().strip()))

Submission status: Runtime Error

Solution code:
# coding: utf-8
 
# R: Right hand side
# L: Left  hand side
def add_stone(stone, pos, searched_stones):
    sample = searched_stones[::[1, -1][pos == "R"]]
    if sample[0] == stone:
        return None
 
    for n, s in enumerate(sample[1:]):
        if s == stone:
            return (stone * (n + 2) + sample[1 + n:])[::[1, -1][pos == "R"]]
 
 
def stones_bfs(searched_stones):
    
    queue = [searched_stones + "0"]
    patterns = [["B", "R"], ["B", "L"], ["W", "R"], ["W", "L"]]
    
    while queue:
        stones = queue.pop(0)
        if (stones.count("W") == len(stones) - 1 |
            stones.count("B") == len(stones) - 1):
            return int(stones[-1])
            
        for p in patterns:
            s = add_stone(*p, stones[:-1])
            if s is not None:
                queue += [s + str(int(stones[-1]) + 1)]
 
print(stones_bfs(input().strip()))

Submission status: Runtime Error

Solution code:
# coding: utf-8


# R: Right hand side
# L: Left  hand side
def add_stone(stone, pos, searched_stones):
    sample = searched_stones[::[1, -1][pos == "R"]]
    if sample[0] == stone:
        return None

    for n, s in enumerate(sample[1:]):
        if s == stone:
            return (stone * (n + 2) + sample[1 + n:])[::[1, -1][pos == "R"]]


def stones_bfs(searched_stones):
    
    queue = [[searched_stones, 0]]
    patterns = [["B", "R"], ["B", "L"], ["W", "R"], ["W", "L"]]
    
    while queue:
        stones = queue.pop(0)
        if (stones[0].count("W") == len(stones[0]) |
            stones[0].count("B") == len(stones[0])):
            return stones[-1]
            
        for p in patterns:
            s = add_stone(*p, stones[0])
            if s is not None:
                queue += [[s, stones[-1] + 1]]

print(stones_bfs(input().strip()))

Submission status: Runtime Error

Solution code:

s=input()

count=0
for i in range(len(s)):
    if s[i]!=s[i+1]: count+=1

print(count)