First dimension / 1D Reversi (AtCoder-2146)

Problem Description

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|≦105
  • 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.

Example

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

Meaning: several pieces are placed in one line. Each time, a w or a B can be placed at both ends of the piece, so that the new W/B to another W/B will become B/W, and at least one time can make all pieces become W/B

Train of thought:

Since the color of the final pieces is required to be the same, each time a piece is placed, it will change to another piece with the same color. Then, the length of the color of the pieces between them does not need to be considered, only how many times of transformation is needed to make the color the same. Therefore, it is necessary to enumerate directly from the front to the back, and record the output of adjacent pieces with different numbers

However, since the color may be the same at the beginning, it is also necessary to record the number of the same color. If the number of the same color is the same as the length of the chess piece, then output 0 directly

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int main() {
    string str;
    cin>>str;
    int len=str.size();

    int same=0,diff=0;
    for(int i=1;i<len;i++){
        if(str[i]!=str[i-1])
            diff++;
        else
            same++;
    }

    if(same==len-1)
        printf("%d\n",0);
    else
        printf("%d\n",diff);

    return 0;
}

 

Posted on Wed, 06 Nov 2019 17:30:23 -0500 by nitko