6479: 1D Reversi(贪心)

6479: 1D Reversi

时间限制: 1 Sec   内存限制: 128 MB
提交: 338   解决: 143
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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.

输入

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

输出

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

样例输入

BBBWW

样例输出

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.


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    char s[100005];
    int sum=0;
    scanf("%s", s);
    int len=strlen(s);
    for(int i=0;i<len-1;i++)
        if(s[i]!=s[i+1])
            sum++;
    printf("%d\n", sum);
    return 0;
} 
/**************************************************************
    Problem: 6479
    User: ldu_reserver201701
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:1092 kb
****************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhjian6666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值