C - 一次元リバーシ / 1D Reversi (思维,“至少需要几次,一定能通过这么多次完成要求”)

题目描述:

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

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|≦105
  • Each 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表示黑色,每次操作可以在两头增加B或W的石头,

增加后新增加的石头到前面/后面同一种颜色的石头之间的另一种颜色石头变色。问最

少需要几步。

思路:

         这种区间头尾操作的题目做过不少,大部分题解都是“一定能在多少步”内完成。

然后这题很明显如果相邻两个石头不一样,那肯定需要通过一次操作使得他们一样(有些模棱两可),

所以有多少不一样的对数就需要多少步。

代码实现:

#include<iostream>
#include<string>
#define LL long long
#define INF 0x3f3f3f3f
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int N=2e5+100;
using namespace std;	
int main()
{
	string str;
	while(cin>>str)
	{
		int len=str.length();
		int ans=0;
		for(int i=1;i<len;i++)
			if(str[i]!=str[i-1])ans++;
		cout<<ans<<endl;
		
	}
	return 0;
} 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值