第19期《atcoder AtCoder Beginner Contest 124C 题解 》

C - Coloring Colorfully Editorial

 / 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

NN tiles are arranged in a row from left to right. The initial color of each tile is represented by a string SS of length NN.

The ii-th tile from the left is painted black if the ii-th character of SS is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

  • 1 \leq |S| \leq 10^51≤∣S∣≤105
  • S_iSi​ is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.


Sample Input 1 Copy

Copy

000

Sample Output 1 Copy

Copy

1

The condition can be satisfied by repainting the middle tile white.


Sample Input 2 Copy

Copy

10010010

Sample Output 2 Copy

Copy

3

Sample Input 3 Copy

Copy

0

Sample Output 3 Copy

Copy

0

语言:C++。

ac代码

#include<bits/stdc++.h>
using namespace std;
int main(){
	
	cin.tie(0);
	ios::sync_with_stdio(0); 
	int g=0;
	string s;
	cin>>s;
	for(int i=1;i<s.size();i++){
		if(s[i]==s[i-1]){
			if(s[i-1]=='0')
			    s[i]='1';
			if(s[i-1]=='1')
			    s[i]='0';
			g++;
		}
	} 
	cout<<g;                                                                                                                                                                                                                                                                                                              
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值