牛客网 第十七届中国计量大学程序设计竞赛(同步赛)(重现赛)B题Broken Pad 暴力+思维
題意:
給你兩個01串,經過兩種操作,1.直接讓第一串經過操作變成目標串;2.可以點擊空白處,即0的地方,使得操作串全部清空為0串,再變為目標串;最終比較兩種方式,哪種需更少步驟,輸出每步點擊的位置。
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 65536K,其他語言131072K
64bit IO Format: %lld
題目描述
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.
分析:
直接暴力遍歷標記兩種方式的狀態,最后比較步數,直接輸出即可。
AC代碼
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int M=1e5+10; int t,tot,num; char a[M],b[M]; bool vis1[M],vis2[M]; int main() {scanf("%d",&t);while(t--){tot=num=0;memset(vis1,false,sizeof(vis1));memset(vis2,false,sizeof(vis2));scanf("%s%s",a+1,b+1);int l=strlen(a+1);for(int i=1; i<=l; i++){int u=b[i]-'0';if(num%2!=u){num++;vis1[i]=true;}}for(int i=1; i<=l; i++){int u=a[i]-'0';int v=b[i]-'0';if((u+tot)%2!=v){tot++;vis2[i]=true;}}if(num+1<tot){printf("0");for(int i=1; i<=l; i++)if(vis1[i])printf(" %d",i);printf("\n");}else{bool flag=false;for(int i=1; i<=l; i++)if(vis2[i]){if(!flag)printf("%d",i),flag=true;elseprintf(" %d",i);}printf("\n");}}return 0; }總結
以上是生活随笔為你收集整理的牛客网 第十七届中国计量大学程序设计竞赛(同步赛)(重现赛)B题Broken Pad 暴力+思维的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥期间能吃鸭肉吗
- 下一篇: 白醋、蜂蜜、黄瓜能减肥吗