T3
CF1675C
Detective Task
Polycarp bought a new expensive painting and decided to show it to his nn friends. He hung it in his room. nn 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.
输入格式
The first number tt ( 1 \le t \le 10^41≤t≤104 ) — 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 2 \cdot 10^52⋅105 ) — a description of the friends' answers, where s_isi 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 2 \cdot 10^52⋅105 .
输出格式
Output one positive (strictly more zero) number – the number of people who could steal the picture based on the data shown.
题意翻译
房屋里有一幅画, nn 个人依次进入房间。前一个人出之后后一个人再进。 nn 个人都出房间后画不见了。每个人只知道自己是不是小偷和他在房间里时画还在不在。
现在审问这 nn 个人,每个人可以回答 1
表示他在房子里时画在, 0
表示不在, ?
表示忘了。小偷会随便回答一个答案,其他人是诚实的。
现在你需要求出,有几个人可能是小偷。
询问数 \leq 10^4≤104 , \sum n\leq 2\times 10^5∑n≤2×105 。保证输入合法。
输入输出样例
输入 #1复制
8 0 1 1110000 ????? 1?1??0?0 0?0??? ??11 ??0??
输出 #1复制
1 1 2 5 4 1 1 3
说明/提示
In the first case, the answer is 11 since we had exactly 11 friend.
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出现的次数,想了半天才发现?其实没有影响,显然对于一个人而言,如果他可能为小偷,那么他前面不能有0,后面不能有1,所以只要n[i-1]==0&&y[s.size()-1]-y[i]==0那么ans++即可。
#include<iostream>
#include<cstring>
using namespace std;
long long t,ans,y[111111*2],n[111111*2];
string s;
int main()
{
cin>>t;
while(t--)
{
cin>>s;
s="a"+s;
memset(y,0,sizeof(y));
memset(n,0,sizeof(n));
int l=s.size();
ans=0;
for(int i=1;i<l;i++)
{
if(s[i]=='1')
{
y[i]=y[i-1]+1;
n[i]=n[i-1];
}
if(s[i]=='0')
{
y[i]=y[i-1];
n[i]=n[i-1]+1;
}
if(s[i]=='?')
{
y[i]=y[i-1];
n[i]=n[i-1];
}
}
for(int i=1;i<l;i++)
{
if(n[i-1]==0&&y[l-1]-y[i]==0)
{
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}