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:
=========================================================================
每当翻转一次,我们翻转中间的部分是不发生任何01位置的改变的,即原本连续的仍然连续
而两端就可能会发生扭转。这样的话,我们可以利用这一性质,拆散连续1或者连续0,最终答案是连续一对1连续一对0的最大值
- # include
- 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
- {
- if(s[i-1]=='0'&&s[i]=='0')
- ans0++;
- else if(s[i-1]=='1'&&s[i]=='1')
- ans1++;
-
- }
- cout<
- }
-
- return 0;
- }
-
相关阅读:
Linux系统LVM逻辑卷
RabbitMQ常见问题
案例:Ajax实现省市联动,选择省后动态显示市和区
【数据结构】动图详解二叉树——堆及堆排序
JSP response对象简介说明
Win11蓝屏代码IRQL NOT LESS OR EQUAL的处理方法
[微前端实战]---036 react16 - 新车排行登录
Azkaban环境配置-尚硅谷大数据培训
Halcon—3D测量算法的那点数学公式和代码实现
ImageDecoder解码GIF、ImageView显示GIF
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126213759