Codeforces Round #787 (Div. 3) C. Detective Task

Codeforces Round #787 (Div. 3)

C. Detective Task

time limit per test:2 seconds
memory limit per test:256 megabyte
input :standard input ;output :standard output

Polycarp bought a new expensive painting and decided to show it to his n friends. He hung it in his room. n of his friends entered and exited there one by one. At one moment there was no more than one person in the room. In other words, the first friend entered and left first, then the second, and so on.

It is known that at the beginning (before visiting friends) a picture hung in the room. At the end (after the nn-th friend) it turned out that it disappeared. At what exact moment it disappeared — there is no information.

Polycarp asked his friends one by one. He asked each one if there was a picture when he entered the room. Each friend answered one of three:

  • no (response encoded with 0);
  • yes (response encoded as 1);
  • can’t remember (response is encoded with ?).

Everyone except the thief either doesn’t remember or told the truth. The thief can say anything (any of the three options).

Polycarp cannot understand who the thief is. He asks you to find out the number of those who can be considered a thief according to the answers.

Input

The first number t (1≤t≤10000) — the number of test cases in the test.

The following is a description of test cases.

The first line of each test case contains one string ss (length does not exceed 200000) — a description of the friends’ answers, where sisi indicates the answer of the ii-th friend. Each character in the string is either 0 or 1 or ?.

The given regularity is described in the actual situation. In particular, on the basis of answers, at least one friend can be suspected of stealing a painting.

It is guaranteed that the sum of string lengths over the entire input data set does not exceed 200000

Output

Output one positive (strictly more zero) number – the number of people who could steal the picture based on the data shown.

Example

input
8
0
1
1110000
?????
1?1??0?0
0?0???
??11
??0??
output
1
1
2
5
4
1
1
3

Note

In the first case, the answer is 1 since we had exactly 1friend.

The second case is similar to the first.

In the third case, the suspects are the third and fourth friends (we count from one). It can be shown that no one else could be the thief.

In the fourth case, we know absolutely nothing, so we suspect everyone.

//题目需要分析有几个会被怀疑
//首先可以进行三个特判(可以从题目得出回答为0一定为真话):
//特判一:因为肯定会有一个嫌疑人,所以如果一个回答的话,就可以直接输出为1;
//特判二:如果最后一位朋友的回答为1,就可以确定他是嫌疑人,因为到最后一位,画肯定是被偷走了,直接输出1。
//特判三:因为回答为0是真话,所以如果第一人回答就为0的话,可以直接确定他就是小偷,输出1;
//然后可以找出第一个说出0的朋友,他说的肯定为真话,所以嫌疑人就是他,或者是在他之前的,记录下他的位置。
//然后找出最后一个说出1的朋友,如果他说的是真话,那嫌疑人就在他后面的朋友,如果他说的是假话,那他就是小偷。记录他的位置。
AC代码
#include <iostream>
#include <string>
using namespace std;
void fun()
{
	string str;
	cin>>str;
	int sum;
	int posR,posL=0,flag=0;//posR记录第一个说出0的位置,posL记录最后一个说出1的位置
	int l=str.size();
	posR=l-1;//将posR初始化为最后一位,posL初始化为第一个;
	if(l==1 || str[l-1]=='1'|| str[0]=='0')//三个特判
	{
		cout<<1<<endl;
		return ;
	}
	for(int i=l-1;i>=0;i--)//从后开始遍历,记录位置,遇到‘0’就直接覆盖;
	{
		if(str[i]=='0')
		{
			posR=i;
		}
	}
		for(int i=0;i<=l-1;i++)//从头开始遍历,记录位置
	{
		if(str[i]=='1' && i<posR)
		{
			posL=i;	
			
		}
	}
		cout<<posR-posL+1<<endl;//两个位置中间的便是嫌疑人(包括这两名);
	
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		fun();
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值