第十七届中国计量大学程序设计竞赛 B -Broken Pad
题目
链接:https://ac.nowcoder.com/acm/contest/7591/B
来源:牛客网
题目描述
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.
输入描述:
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| ≤ 10^510
5
), indicating the current state of the card.
The second line contains a string b (|b| = |a|), indicating the state required by ZH.
输出描述:
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.
示例1
输入
2
10110
10000
110101
000000
输出
3 5
0
说明
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.
题意:
本题意义其实也就是给你一串牌让它变成另一串牌,而只有两个操作,一个是点当前牌,当前牌及后面的牌全部翻转。另一个是全部变为0。问最小的操作顺序。
思路
本题的一种思路呢就是我们做两种情况讨论1.不进行全变0操作 2.进行全变0操作。将这两种操作的顺序分别存入数组。看哪个数量少便输出哪个。
至于如何优化模拟翻牌呢。可以用一个num来存放当前这张牌之前已经翻了几次。那么如果num%2=0,则不变。若num%2=1,那么需要翻面。
代码:
#include <bits/stdc++.h>
using namespace std;
vector<int>ans1,ans2;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ans1.clear(),ans2.clear();
string s1,s2;int num1=0,num2=0;
cin>>s1>>s2;
//不清0.
for(int i=0;i<s1.size();i++)
{
if(num1%2==1)
{
if(s1[i]=='1') s1[i]='0';
else s1[i]='1';
}
if(s1[i]!=s2[i])
{
num1++;
ans1.push_back(i+1);
}
}
//清0.
ans2.push_back(0);
for(int i=0;i<s2.size();i++)
{
if(s2[i]!=('0'+(num2%2)))
{
num2++;
ans2.push_back(i+1);
}
}
if(ans2.size()<=ans1.size())//输出ans2.
{
for(int i=0;i<ans2.size();i++)
{
printf("%d ",ans2[i]);
}puts("");
}
else
{
for(int i=0;i<ans1.size();i++)
{
printf("%d ",ans1[i]);
}puts("");
}
}
return 0;
}