Penalty(逻辑分析)

简介: Penalty(逻辑分析)

Consider a simplified penalty phase at the end of a football match.


A penalty phase consists of at most 10 kicks, the first team takes the first kick, the second team takes the second kick, then the first team takes the third kick, and so on. The team that scores more goals wins; if both teams score the same number of goals, the game results in a tie (note that it goes against the usual football rules). The penalty phase is stopped if one team has scored more goals than the other team could reach with all of its remaining kicks. For example, if after the 7-th kick the first team has scored 1goal, and the second team has scored 3 goals, the penalty phase ends — the first team cannot reach 3goals.


You know which player will be taking each kick, so you have your predictions for each of the 10 kicks. These predictions are represented by a string ss consisting of 10 characters. Each character can either be 1, 0, or ?. This string represents your predictions in the following way:


  • if sisi is 1, then the ii-th kick will definitely score a goal;
  • if sisi is 0, then the ii-th kick definitely won't score a goal;
  • if sisi is ?, then the ii-th kick could go either way.


Based on your predictions, you have to calculate the minimum possible number of kicks there can be in the penalty phase (that means, the earliest moment when the penalty phase is stopped, considering all possible ways it could go). Note that the referee doesn't take into account any predictions when deciding to stop the penalty phase — you may know that some kick will/won't be scored, but the referee doesn't.


Input


The first line contains one integer tt (1≤t≤1000) — the number of test cases.


Each test case is represented by one line containing the string ss, consisting of exactly 10 characters. Each character is either 1, 0, or ?.


Output


For each test case, print one integer — the minimum possible number of kicks in the penalty phase.


Example


Input

1. 4
2. 1?0???1001
3. 1111111111
4. ??????????
5. 0100000000


Output

1. 7
2. 10
3. 6
4. 9


Note


Consider the example test:


In the first test case, consider the situation when the 1-st, 5-th and 7-th kicks score goals, and kicks 2, 3, 4 and 6 are unsuccessful. Then the current number of goals for the first team is 3, for the second team is 0, and the referee sees that the second team can score at most 2 goals in the remaining kicks. So the penalty phase can be stopped after the 7-th kick.


In the second test case, the penalty phase won't be stopped until all 10 kicks are finished.


In the third test case, if the first team doesn't score any of its three first kicks and the second team scores all of its three first kicks, then after the 6-th kick, the first team has scored 0 goals and the second team has scored 3 goals, and the referee sees that the first team can score at most 2 goals in the remaining kicks. So, the penalty phase can be stopped after the 6-th kick.


In the fourth test case, even though you can predict the whole penalty phase, the referee understands that the phase should be ended only after the 9-th kick.


题目分析,就是我们可以明确知道是否进球通过0,1,这时候我们可以把?分成a队必进,b队不进,然后计算这种情况,第二种就是b队必进,a队必不进,最后把这2种情况比较大小取小的。


听懂了记得给个赞鼓励一下,码字不易,用爱发电。


上ac代码。

f58230e9f063709cf3167704f4efdf14.gif


有事你就q我;QQ2917366383


学习算法

#include<iostream>
#include<cmath>
using namespace std;
  string s;
inline int zhuq1a()//支持a队
{
  int a1=5,b1=5,a1s=0,b1s=0;//a1表示a队剩下的进球机会,b1同理,a1s则是a队已经得到的分数,b1s同理
  for(int i=0;i<=9;i++)
  {
    if(i%2==0)
    {a1--;//别忘了处理剩下的进球机会
    if(s[i]=='?')
    a1s++;  //默认进球    
    else 
    a1s+=(int)s[i]-'0';//字符转换为数字
      }
    else
    {     
      b1--;
      if(s[i]=='?')
      {//什么都不做,即默认不进球
       } 
       else
       b1s+=(int)s[i]-'0';
     } 
     if(b1s+b1<a1s)return i+1;//这里i是字符串下标,转换成点球的轮数需要+1    
  }
  return 10;
}
inline int zhuq1b()//支持b队,同上
{
  int a1=5,b1=5,a1s=0,b1s=0;
  for(int i=0;i<=9;i++)
  {
    if(i%2==0)
    {a1--;
    if(s[i]=='?')
    {}    
    else 
    a1s+=(int)s[i]-'0';
      }
    else
    {     
      b1--;
      if(s[i]=='?')
      {
        b1s++;
       } 
       else
       b1s+=(int)s[i]-'0';
     } 
     if(a1s+a1<b1s)return i+1;    
  }
  return 10;
}
int main()
{
  int t;
  cin>>t;
  while(t--)//CF多组数据
  {
        cin>>s;
cout<<min(zhuq1a(),zhuq1b())<<endl;
  }
}


相关文章
|
JSON 小程序 Java
微信小程序系列之-微信小程序授权登录
java后端对微信小程序进行授权登录操作
|
缓存 算法 Java
C++ 编程基础总结
C++ 编程基础总结
458 0
|
3天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
13天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
509 203
|
5天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
730 157
|
11天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。