7-2 Broken Pad (20 分)
1.题目描述:
The party began, the greasy uncle was playing cards, the fat otaku was eating, and the little beauty was drawing.
Playing cards is an indispensable and irreplaceable activity for parties. In order to be more impartial and prevent magician YHH from cheating when shuffling cards, and give full play to his mind-reading advantages at the same time, psychologist ZH proposed to use a pad to play cards.
However, ZH found that the touch screen of the pad he was assigned was malfunctioning. Every time he clicked a card, this card and all the cards behind it would be selected. As we all know, the effect of choosing a card is equivalent to changing the state of the card, that is, if the card was initially selected, it would become unselected, and if it was unselected, it would become selected. Besides, there is another operation, which is to click on the blank space, so that all the cards will become unselected.
Now ZH needs to select some cards to play, but due to the malfunctioning of the touch screen, he cannot simply choose the cards he wants to choose. Now he has used the blind trick to secretly ask netizens which positions to click to choose the cards he wants, please help him!
Given two 01-strings a and b, which represent the current state of the card and the state required by ZH. 0 represents unselected, and 1 represents selected. Now you are asked to give a plan with the smallest number of tap times.
Input Specification:
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:
The first line contains a string a (1 ≤ ∣a∣ ≤ 105), indicating the current state of the card.
The second line contains a string b (∣b∣=∣a∣), indicating the state required by ZH.
Output Specification:
For each test case, print one line contains the minimum number of integers indicating the position ZH should tap in non-decreasing order.
Please note that number 0 is indicating the blank space, and it's guaranteed that the solution of all the test cases is unique.
Sample Input:
2
10110
10000
110101
000000
结尾无空行
Sample Output:
3 5
0
结尾无空行
Hint:
For the first sample, a is "10110", and b is "10000", then ZH needs first to tap the position 3 (based on 1) to make the state become "10001", and then tap the position 5 to make the state become "10000", so you should tell him 3 and 5.
2.代码展示:
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+5;
//用于计算操作数
int cal(string s1,string s2,int c[]){
int count1=0,num=0;
int flag=1;//用于标记状态是否改变
for(int i=0;s1[i];i++){
//如果此时片初始状态和终止状态不同,且卡片初始状态未改变
if((s1[i]!=s2[i])&&flag){
c[num++]=i+1;//存储操作过程
count1++;//统计操作数
flag=0;//改变状态
}
//如果此时片初始状态和终止状态相同,且卡片初始状态改变
else if(!flag&&(s1[i]==s2[i])){
c[num++]=i+1;//存储操作过程
count1++;//统计操作数
flag=1;//改变状态
}
}
return count1;
}
int main(){
int T;//T为测试用例的数量
cin>>T;
while(T--){
string s1,s2,s3;
int a[MAX],b[MAX];
cin>>s1>>s2;
s3=s1;
int count1=0,count2=0;
//1.按清零操作
for(int i=0;s1[i];i++)s1[i]='0';//将卡片初始状态全部清零
count1++;//操作数加1
count1=cal(s1,s2,a);//进行操作数的计算
//2.不按清零操作
count2=cal(s3,s2,b);//进行操作数的计算 //进行操作数的计算
//比较清零与不清零的操作数,看看谁是最小值
if(count1<count2){
cout<<"0";
for(int i=0;i<count1;i++)cout<<" "<<a[i];
}else {
for(int i=0;i<count2;i++){
if(i==count2-1)cout<<b[i];
else cout<<b[i]<<" ";
}
}
cout<<endl;
}
return 0;
}
原题链接:PTA | 程序设计类实验辅助教学平台
7-2 Broken Pad (20 分)的更多相关文章
- 【PAT甲级】1084 Broken Keyboard (20 分)
题意: 输入两行字符串,输出第一行有而第二行没有的字符(对大小写不敏感且全部以大写输出). AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #inclu ...
- 1084 Broken Keyboard (20 分)
1084 Broken Keyboard (20 分) On a broken keyboard, some of the keys are worn out. So when you type so ...
- pat 1084 Broken Keyboard(20 分)
1084 Broken Keyboard(20 分) On a broken keyboard, some of the keys are worn out. So when you type som ...
- 1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- 【刷题-PAT】A1112 Stucked Keyboard (20 分)
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...
- 抛弃EF,20分构建一个属于自己的ORM框架
Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11 文章 - 0 评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- #020PAT 没整明白的题L1-009 N个数求和 (20 分)
后面的测试点过不去,两个错误一个超时. 目前未解决 L1-009 N个数求和 (20 分) 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...
- L1-023 输出GPLT (20 分)
L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...
- PAT 乙级 1074 宇宙无敌加法器 (20 分)
1074 宇宙无敌加法器 (20 分) 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每 ...
随机推荐
- node使用multer进行文件上传
开场白 在平时的业务中,我们很多使用都会有文件上传这个功能. 今天分享一下使用 node+element-ui实现一下文件上传. 请个人大佬指点一番~~~.批评的时候稍微轻一点. 毕竟我心里承受能力弱 ...
- RPA的概念和优势
大多数人每天都会使用到一些机器人流程自动化的工具,例如读取邮件和系统,计算.生成文件和报告.而在未来,那些你不想做的枯燥的工作,也许真的可以不做了,重复化.标准化的工作都可以让机器人帮你完成.想必此刻 ...
- Javaweb学习笔记第十六弹--Vue、Element
Vue(一套前端框架,MVVM主要用于实现数据的双向绑定) Vue快速入门 //新建HTML页面,引入Vue.js文件 <script src="js.Vue.js"> ...
- ElasticSearch 实现分词全文检索 - Scroll 深分页
目录 ElasticSearch 实现分词全文检索 - 概述 ElasticSearch 实现分词全文检索 - ES.Kibana.IK安装 ElasticSearch 实现分词全文检索 - Rest ...
- 干货 | BitSail Connector 开发详解系列一:Source
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 BitSail 是字节跳动自研的数据集成产品,支持多种异构数据源间的数据同步,并提供离线.实时.全量.增量场景下全 ...
- 【读书笔记】组合计数中的行列式方法 专题3 完美匹配: the Pfaffian method
目录 专题3-Perfect matchings: the Pfaffian method 一些定义 用2×1的砖密铺a×b的大矩形的方法数 专题3-Perfect matchings: the Pfa ...
- 屎上最全vue-pdf+Springboot与aspose-words整合,开箱即用
前言 ⏲️本文阅读时长:约10分钟 主要目标: 1.实现Springboot与aspose-words整合,填充word模板并转化PDF; 2.前端vue整合vue-pdf实现PDF预览及下载 wor ...
- Sokit(TCP/UDP调试工具)
下载:http://www.winwin7.com/soft/56522.html#xiazai Sokit中文版是一款免费开源的TCP / UDP 测试(调试)工具,它主要可以用于接收和发送TCP/ ...
- 一文详解扩散模型:DDPM
作者:京东零售 刘岩 扩散模型讲解 前沿 人工智能生成内容(AI Generated Content,AIGC)近年来成为了非常前沿的一个研究方向,生成模型目前有四个流派,分别是生成对抗网络(Gene ...
- jQuery绑定方法
流程/*** * 1.文件命名 * jquery.banner.1.0.0.js * jquery.banner.js * 2.添加匿名函数 * (function(){})() * 3.给插件前后添 ...