Topic URL
https://ac.nowcoder.com/acm/contest/46800/A
Attached picture
Generally written, just for reference
The idea is
Record the number of games won by both sides
1. When the score difference between the two sides exceeds 3, there is no need to compare, and it will end directly
2. Calculate whether one side can catch up with or even surpass the opponent if they win all the next games. If yes, continue to execute the cycle; if not, end the game directly
3. At the end of all games, if the scores of the two sides are equal, it will be a tie, and the output will be -1.
#include <iostream>
using namespace std;
int main() {
int t,m,n,flag,mm,nn;
string a;
cin>>t;
while(t--){
m=0;n=0;flag=0;mm=0;nn=0;
cin>>a;
for(int i=0;i<=9;i++){
if(i%2==0){
if(a[i]=='1'){
m++;
}
mm++;
}else{
if(a[i]=='1'){
n++;
}
nn++;
}
if((m-n>=3||n-m>=3)&&i%2==1){
flag=i+1;
break;
}
if(m>n+5-nn||n>m+5-mm){
flag=i+1;
break;
}
}
if(m==n){
cout<<"-1"<<endl;
}else if(flag!=0){
cout<<flag<<endl;
}
}
}