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 toalternate. 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.
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.
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.
5 rbbrr
1
5 bbbbb
2
3 rbr
0
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
题意:
给定一组字符串,不能够使相同的相邻,问最小操作次数
思路:
WA:
分组统计,既然相同的不在一起,那后面的排序应该和前两个的格式一样,如果前两个是 rb 后面的也都是 rb,此时思路有bug。。
AC:
单独统计奇数和偶数位r, b 的个数,最后比较。。
AC CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e5;
int AC {
int n, xr, yr, xb, yb;
char a[MYDD];
scanf("%d", &n);
scanf("%s", a + 1);
xr = yr = xb = yb = 0;
for(int j = 1; j <= n; j++) {
if(j % 2) {//第奇数个字母
if(a[j] == 'r') xr++;
else xb++;
} else {
if(a[j] == 'r') yr++;
else yb++;
}
}
int Answer = min(max(xr, yb), max(xb, yr));
printf("%d\n", Answer);
return 0;
}
MY WA code(卡在第四组数据):
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define AC main()
using namespace std;
const int MYDD = 1103 + 1e5;
int AC {
int n, Ans = 0, cntr = 0, cntb = 0;
char a[MYDD];
scanf("%d", &n);
for(int j = 0; j < MYDD ; j++) a[j] = '0';
scanf("%s", a);
for(int j = 0; j < n; j+=2) {//已经默认分组
if(a[j] == a[j+1]) {
Ans++;
} else {//第奇数个字母的情况统计
if(a[j] == 'r') cntr++;
else cntb++;
}
}
printf("%d\n", Ans + (cntb < cntr ? cntb:cntr));
return 0;
}
/*
13
rbbbrbrrbrrbb
Output
4
Answer
3
*/