C - 一次元リバーシ1D Reversi

    xiaoxiao2022-07-03  129

    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 SS. There are |S| (the length of SS) stones on the board, and each character in SS represents the color of the ii-th (1≦i≦|S|1≦i≦|S|) stone from the left. If the ii-th character in SS is B, it means that the color of the corresponding stone on the board is black. Similarly, if the ii-th character in SS 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|≦1051≦|S|≦105Each character in SS is B or W.

    Input

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

    SS

    Output

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


    Sample Input 1 Copy

    Copy

    BBBWW

    Sample Output 1 Copy

    Copy

    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 Copy

    Copy

    WWWWWW

    Sample Output 2 Copy

    Copy

    0

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


    Sample Input 3 Copy

    Copy

    WBWBWBWBWB

    Sample Output 3 Copy

    Copy

    9

    题意: 给你一个字符串w表示白色,b表示黑色,你可以在两端添加字符添加w可以把字符串中据你添加的w最近的一个w之间的黑色全部变成白色,添加B同理。问你最少添加多少字符让所有的字符都相等。

    思路:找有多少组相邻的但是不相等的字符。

    代码:  

    #include<bits/stdc++.h> using namespace std; char s[100009]; int main() { scanf("%s",&s); int n=strlen(s); int k=0; for(int i=0;i<n-1;i++) { if(s[i]!=s[i+1]) { k++; } } cout<<k<<endl; }

     

    最新回复(0)