Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches mig ...
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.
Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.
Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.
The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.
Output
Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.
Examples
Input5Output
rbbrr
1Input
5Output
bbbbb
2Input
3Output
rbr
0
題意
有n個字元,要麼是r,要麼是b,要使其成為交替出現的字元所組成的字元串
如:rbrbrbr 或 brbrbrbrb……
每次你可以修改一個字元,或者交換兩個字元的位置。
問你最少改變的次數是多少
#include<bits/stdc++.h> using namespace std; int main() { char a[100005]; int n,s1=0,s2=0,minn,i; scanf("%d %s",&n,a); for(i=0;i<n;i++) { if(i%2) //rbrbrbr { if(a[i]!='r') s1++; } else { if(a[i]!='b') s2++; } } minn=abs(s1-s2)+min(s1,s2); //他們的差(直接變色)+奇數位和偶數位錯誤的最小值(交換) s1=s2=0; for(i=0;i<n;i++) { if(i%2) //brbrbrb { if(a[i]!='b') s1++; } else { if(a[i]!='r') s2++; } } minn=min(minn,abs(s1-s2)+min(s1,s2)); cout<<minn<<endl; }