当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 【CodeForces 719B】Anatoly and Cockroaches

【CodeForces 719B】Anatoly and Cockroaches

2018年04月15日  | 移动技术网IT编程  | 我要评论

必胜韩剧,大宅小家,楼俊毅

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

Input
5
rbbrr
Output
1
Input
5
bbbbb
Output
2
Input
3
rbr
Output
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;
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网