B. MIN-MEX Cut

题目如下:

A binary string is a string that consists of characters 0 and 1 Let MEX

of a binary string be the smallest digit among 0, 1, or 2 that does not occur in the string. For example, MEX of 001011 is 2, because 0 and 1 occur in the string at least once, MEX of 1111 is 0, because 0 and 2 do not occur in the string and 0<2

.

A binary string s

is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.

A string a

is a substring of a string b if a can be obtained from b

by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

What is the minimal sum of MEX

of all substrings pieces can be?

Input

The input consists of multiple test cases. The first line contains a single integer t

(1≤t≤104

) — the number of test cases. Description of the test cases follows.

Each test case contains a single binary string s

(1≤|s|≤105

).

It's guaranteed that the sum of lengths of s

over all test cases does not exceed 105

.

Output

For each test case print a single integer — the minimal sum of MEX

of all substrings that it is possible to get by cutting s optimally.

AC代码如下:

#define maxn 100010
#include<stdio.h>
#include<string.h>
char s[maxn];
int main()
{
	int t,i;
	scanf("%d", &t);
	while (t--)
	{
		memset(s, 0, sizeof s);
		scanf("%s", s, maxn);
		int flag1 = 0;//是否有1
		int flag2 = 0;//0是否连续:0->没有出现0,1,2—》连续,3—》不连续
		for (i = 0; i < strlen(s); i++)
		{
			if (s[i] == '0')
			{
				if(flag2==0)
				flag2 = 1;
				if (flag2 == 2)
					flag2 = 3;
			}
			if (s[i] == '1')
			{
				flag1 = 1;
				if (flag2 == 1)
				{
					flag2 = 2;
				}
			}
		}
		if (flag1 == 0 && flag2 != 3 && flag2 != 0)//无1且0连续
		{
			printf("1\n");
		}
		else if (flag1 == 1 && flag2 != 3 && flag2 != 0)//有1且0连续
		{
			printf("1\n");
		}
		else if (flag1 == 1 && flag2 == 3)//有1且0不连续
		{
			printf("2\n");
		}
		else if(flag1 == 1 && flag2==0)
		{
			printf("0\n");
		}
	}
	return 0;
}

代码解析:

核心思路:确定0的位置(0是否连续出现);确定是否出现1;

0连续——》输出1

0不连续——》输出2

无0但有1——》输出0

相信根据代码与代码解析就可以A这个题了!

ps:晚上宿舍一起在努力,奋斗的动力又再次蓄上了力!

ACM越来越近,奋斗努力!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Joanh_Lan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值