Tokitsukaze and Good 01-String (easy version)

Tokitsukaze and Good 01-String (easy version) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目描述

This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.

Tokitsukaze has a binary string ss of length nn , consisting only of zeros and ones, nn is even.

Now Tokitsukaze divides ss into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, ss is considered good if the lengths of all subsegments are even.

For example, if ss is "11001111", it will be divided into "11", "00" and "1111". Their lengths are 22 , 22 , 44 respectively, which are all even numbers, so "11001111" is good. Another example, if ss is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are 33 , 22 , 22 , 33 . Obviously, "1110011000" is not good.

Tokitsukaze wants to make ss good by changing the values of some positions in ss . Specifically, she can perform the operation any number of times: change the value of s_isi​ to '0' or '1'( 1 \leq i \leq n1≤i≤n ). Can you tell her the minimum number of operations to make ss good?

输入格式

The first contains a single positive integer tt ( 1 \leq t \leq 10\,0001≤t≤10000 ) — the number of test cases.

For each test case, the first line contains a single integer nn ( 2 \leq n \leq 2 \cdot 10^52≤n≤2⋅105 ) — the length of ss , it is guaranteed that nn is even.

The second line contains a binary string ss of length nn , consisting only of zeros and ones.

It is guaranteed that the sum of nn over all test cases does not exceed 2 \cdot 10^52⋅105 .

输出格式

For each test case, print a single line with one integer — the minimum number of operations to make ss good.

题意翻译

题面

给定一个长度为 nn 的 0,10,1 串,如 “11001111”。将这个字符串分为“11”,“00”,“1111”,它们的长度都是偶数,我们就称这个 0,10,1 串为好的。反之,如果分分段后每段长度不是偶数,我们就称其为不好的。

本题有多组数据,对于么组数据,输出将一个不好的 0,10,1 串变成好的所需要的最少操作次数。

对于每次操作,可以将 0,10,1 串中任意的“0”或“1”修改成“0”或“1”。

输入格式

第一行输入一个正整数 TT,表示数据组数。

对于每组数据,第一行为一个偶数 nn,表示 0,10,1 串的长度。

第二行包含一个长度为 nn 的 0,10,1 串。

1 \le t \le 10000,2 \le n \le 2 \times 10^51≤t≤10000,2≤n≤2×105

输出格式

对于每个测试用例,输出一行,为最小操作数。

输入输出样例

输入 #1复制

5
10
1110011000
8
11001111
2
00
2
11
6
100110

输出 #1复制

3
0
0
0
3

说明/提示

In the first test case, one of the ways to make ss good is the following.

Change s_3s3​ , s_6s6​ and s_7s7​ to '0', after that ss becomes "1100000000", it can be divided into "11" and "00000000", which lengths are 22 and 88 respectively. There are other ways to operate 33 times to make ss good, such as "1111110000", "1100001100", "1111001100".

In the second, third and fourth test cases, ss is good initially, so no operation is required.

 1.这个题目也很简单啦,只要贪心每一位即可,我们循坏找1或者0的个数,如果当前1的个数是奇数,那么我们就把后面因为赋值为1,使它变成偶数。

2.依次类推,我们就能得到最优解。

3.需要注意的时候,循坏的时候出来的那个值不是前面的状态,你改了它,你要往后走一位。

#include<stdio.h>
#define N 100010
char str[100010];
int res[10010];
int slove(int n)
{
	int i,j,zero,one,count=0;
	for(i=0;i<n;)
	{
		zero=0;
		one=0;
		while(i<n&&str[i]=='1') 
		{
			one++;i++;
		}
		if(one%2==1)
		{
			str[i]='1';count++;
			i++;
		}
		
		while(i<n&&str[i]=='0') 
		{
			zero++;
			i++;
		}
		if(zero%2==1)
		{
			str[i]='0';count++;
			i++;
		}
		
	//	printf("%d %d\n",one,zero);
	}
	return count;
}
int main()
{
	int t,n,i,j;
	scanf("%d",&t);
	for(i=0;i<t;i++)
	{
		scanf("%d",&n);
		scanf("%s",str);
		res[i]=slove(n);
	}
	for(i=0;i<t;i++)
	{
		printf("%d\n",res[i]);
	}
}

Accepted!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值