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.

Example
input
6
01
1111
01100
101
0000
01010
output
1
0
2
1
1
2
思路:就把1当成分割线求有多少关于0的单词就可以了。
代码:

#include<stdio.h>
#include<string.h>
int main()
{
	char s[101010];
	int t,i,j,l;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",s);
		l=strlen(s);
		int sum=0;
		int spm=0;
		for(i=0;i<l;i++)
		{
			if(s[i]=='1')
			{
				sum++;
			}
			if(s[i]=='0')
			{
				spm++;
			}
		}
		int ans=0,g=1;
		//printf("<<%d\n",ans);
		if(sum==0&&spm!=0)
		{
			printf("1\n");
		}
		else if(sum!=0&&spm==0)
		{
			printf("0\n");
		}
		else if(sum!=0&&spm!=0)
		{
			if(s[l-1]=='1')
			{
				for(i=l-1;i>=0;i--)
				{
					if(s[i]=='1'&&s[i-1]!='1')
					{
						l=i;
						break;
					}
				}
			}
			for(i=0;i<l;i++)
			{
				if(s[i]=='0')
				{
					g=0;
				}
				if(s[i]=='1'&&g==0)
				{
					ans++;
					g=1;
				}
			}
			//printf("<<%d\n",ans);
			if(ans+1>=2) printf("2\n");
			else printf("1\n");
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值