B. Reverse Binary Strings

B. Reverse Binary Strings

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string ss of even length nn. String ss is binary, in other words, consists only of 0's and 1's.

String ss has exactly n2n2 zeroes and n2n2 ones (nn is even).

In one operation you can reverse any substring of ss. A substring of a string is a contiguous subsequence of that string.

What is the minimum number of operations you need to make string ss alternating? A string is alternating if si≠si+1si≠si+1 for all ii. There are two types of alternating strings in general: 01010101... or 10101010...

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤1052≤n≤105; nn is even) — the length of string ss.

The second line of each test case contains a binary string ss of length nn (si∈si∈ {0, 1}). String ss has exactly n2n2 zeroes and n2n2 ones.

It's guaranteed that the total sum of nn over test cases doesn't exceed 105105.

Output

For each test case, print the minimum number of operations to make ss alternating.

Example

input

Copy

3
2
10
4
0110
8
11101000

output

Copy

0
1
2

Note

In the first test case, string 10 is already alternating.

In the second test case, we can, for example, reverse the last two elements of ss and get: 0110 →→ 0101.

In the third test case, we can, for example, make the following two operations:

  1. 11101000 →→ 10101100;
  2. 10101100 →→ 10101010.

=========================================================================

每当翻转一次,我们翻转中间的部分是不发生任何01位置的改变的,即原本连续的仍然连续

而两端就可能会发生扭转。这样的话,我们可以利用这一性质,拆散连续1或者连续0,最终答案是连续一对1连续一对0的最大值

# include<iostream>
using namespace std;

int main ()
{

    int t;
    cin>>t;

    while(t--)
    {
        int n;
        cin>>n;
        int ans0=0,ans1=0;

        string s;
        cin>>s;
        for(int i=1;i<n;i++)
        {
            if(s[i-1]=='0'&&s[i]=='0')
                ans0++;
            else if(s[i-1]=='1'&&s[i]=='1')
                ans1++;

        }
        cout<<max(ans1,ans0)<<endl;
    }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值