zoukankan      html  css  js  c++  java
  • I

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

    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

    题解:只需统计字符串中不同区间的个数即可

    想想自己当时想那么麻烦,真的有种想呵呵的感觉,继续加油吧少年!

    AC代码

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int main()
     5 {
     6     char s[100005];
     7     scanf("%s", s);
     8     int len = strlen(s);
     9     int ans = 0;
    10     for(int i = 1; i < len; i++)
    11     {
    12         if(s[i] != s[i-1])
    13             ans++;
    14     }
    15     printf("%d
    ", ans);
    16 
    17     return 0;
    18 }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    4. 单向循环链表
    3. 单向链表
    2. 队列的实现
    1. 时间复杂度(大O表示法)以及使用python实现栈
    Ubuntu16.04LTS安装集成开发工具IDE: CodeBlocks 和Eclipse-cdt
    Ubuntu16.04搭建各种开发环境的IDE: QT5 , CodeBlocks ,eclipse-cdt, PyCharm
    python2和python3 分别连接MySQL的代码
    面向对象基础
    文件的处理
    离散表(哈希表),深浅拷贝
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8560431.html
Copyright © 2011-2022 走看看