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;
  }
}


相关文章
|
缓存 自动驾驶 物联网
C-RAN——无线接入网架构优化 | 带你读《5G时代的承载网》之十八
C-RAN 是根据现网条件和技术进步的趋势,提出的新型无线接入网构架, 是基于集中化处理(Centralized Processing)、协作式无线电(Collaborative Radio)和实时云计算构架(Real-time Cloud Infrastructure)的绿色无线接 入网构架(Clean System)。其本质是通过实现减少基站机房数量,减少能耗, 采用协作化、虚拟化技术,实现资源共享和动态调度,提高频谱效率,以达到 低成本、高带宽和灵活度的运营。
C-RAN——无线接入网架构优化 | 带你读《5G时代的承载网》之十八
|
2月前
|
人工智能 自然语言处理 机器人
程序员节限时福利:21元拿下好域名
项目写到一半,域名还没定?1024开发者节推出专属后缀:`.chat`(AI/客服)、`.run`(运行环境)、`.team`(开发团队),首年统一21元,直降40%–58%!支持万网CLI自然语言查域名、比价、下单。
程序员节限时福利:21元拿下好域名
|
2月前
|
存储 弹性计算 运维
阿里云优惠活动大全整理:高性价比云服务器清单及活动参与教程
对于个人开发者、学生、小微企业来说,云服务器的成本直接影响项目落地与预算规划。阿里云长期推出多类型优惠活动,覆盖入门建站、程序开发、测试环境、轻量业务等场景,不同活动面向的用户群体、产品规格、价格门槛差异明显。很多新手在刚接触时,分不清 阿里云轻量应用服务器 、 阿里云ECS云服务器 经济型实例各自适合什么场景,也不清楚活动参与条件、实名要求、限购规则,很容易错过低价名额,或是误选不匹配业务的机型,造成资源浪费。
175 1
|
2月前
|
人工智能 编解码 JSON
AI 漫剧动态视频工程化生产实战:图生视频工作流、时序稳定性、显存调优与完整流水线落地
本文详解AI漫剧图生视频的底层技术与工程化实践:基于Wan2.2-Animate(DiT+MoE+3D-VAE)架构,强调低运动强度(motion 3–6)、短片段(3–6秒)、模块化工作流(6大可调试模块)、时序防抖及严格资产版本管理,兼顾效果稳定与消费级硬件可行性。
511 0
|
4月前
|
缓存 自然语言处理 语音技术
浏览器端多语言音色迁移工程实践:OpenVoice V2 FP16 ONNX、WebGPU 与 ModelScope 模型分发
Timeline Studio 是一款浏览器端多语言声音克隆与音色迁移工具,基于 OpenVoice V2 FP16 ONNX 模型,集成 ONNX Runtime Web、WebGPU/WASM 加速、双源模型分发(ModelScope/HF)、Cache Storage 缓存及 IndexedDB 声音档案。支持中文等11种语言,实现TTS语音生成+音色独立迁移,全程本地处理,无需服务器依赖。(239字)
|
5月前
|
人工智能 安全 Linux
最新版 Claude Code(命令行 AI 编程助手) 功能介绍及接入阿里云百炼指南
Claude Code是Anthropic官方推出的**命令行原生AI编程助手**,主打无界面轻量化运行、全工程代码理解、自动化重构调试、长文本项目解析能力,凭借超强的代码上下文理解、低幻觉输出、全链路工程处理能力,成为全球开发者主流终端编程工具。但原版官方服务存在网络访问受限、海外计费昂贵、模型适配单一等问题,极大限制国内开发者常态化使用。
982 0
|
6月前
|
人工智能 自然语言处理 前端开发
万小智 AI 建站:开发者从零到生产级官网的完全实战指南
本文面向后端/全栈开发者、技术负责人及独立开发者,提供“AI建站+定制开发+生产运维”一体化工作流。10分钟生成官网骨架,支持代码嵌入、域名备案、Webhook集成与多语言部署,大幅降低交付与维护成本,让开发者专注核心业务逻辑。(239字)
|
5月前
|
存储 人工智能 运维
当 AI 真正走进企业文件区:CDE Agent 如何把"权限与审计"织进每一次对话
CDE Agent 是阿里云盘企业版推出的合规型AI助手,将权限控制、空间隔离与全链路审计深度内嵌于产品底层。它不突破员工权限边界,所有产出自动归档至指定目录,并复用原生审计体系,确保金融、医疗等高监管行业AI应用全程可控、可溯、可管。
|
5月前
|
JavaScript
Vue 3 环境搭建与 Vite 项目初始化
本指南详解Vue 3 + Vite项目初始化:推荐`vue-ts`模板,精简起步,避免过早引入复杂依赖;强调`&lt;script setup lang=&quot;ts&quot;&gt;`语法、合理目录结构及必做生产构建验证,助你快速搭建稳健开发基础。(239字)
666 0
|
6月前
|
人工智能 JavaScript 数据挖掘
让你的Hermes能力翻倍:从skills.sh装技能包的完整指南
Hermes Agent装好了但总觉得不够好用?问题可能不在模型,在技能包。本文详解skills.sh这个有600+社区技能的Agent应用商店,手把手教你两种集成方式——npx skills add配合external_dirs配置的通用方案,以及hermes skills install原生一键安装的省事路线,让你的Hermes真正"懂行"
1111 0