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
or1
.
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;
}