• 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的最大值

    1. # include
    2. using namespace std;
    3. int main ()
    4. {
    5. int t;
    6. cin>>t;
    7. while(t--)
    8. {
    9. int n;
    10. cin>>n;
    11. int ans0=0,ans1=0;
    12. string s;
    13. cin>>s;
    14. for(int i=1;i
    15. {
    16. if(s[i-1]=='0'&&s[i]=='0')
    17. ans0++;
    18. else if(s[i-1]=='1'&&s[i]=='1')
    19. ans1++;
    20. }
    21. cout<
    22. }
    23. return 0;
    24. }

  • 相关阅读:
    线程池概述
    利用Python进行数据分析——函数部分
    基于Basic auth 的一个C# 示例
    面试题 05.02. 二进制数转字符串
    腾讯云对象存储 COS搭建个人网站
    用ChatGPT,绘制一个账号系统的C4架构图
    feign 和 openFeign 的区别
    [ERROR] mariadbd: The table 'INNODB_BUFFER_PAGE' is full
    Zookeeper系列——1Zookeeper简介及部署
    IPWorks EDI Translator Delphi Edition
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126213759