当前位置: 首页 > 工具软件 > Tap Pad > 使用案例 >

每日一练,Broken Pad (20 分)

丘畅
2023-12-01

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∣ ≤ 105), 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.

样例输入:

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),所有牌的状态变成未选中。

要使用最少的点击次数将手中的牌从一种状态转换成另一种状态,需要判断直接转换和点击空格后再转换两种转换方式哪种总点击次数更少。因为点击前面的牌会影响后面的牌,所以转换过程从前到后,同时使用数组记录每次点击的位置。

代码展示:

#include<iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;

    while(t--)
    {
        string s1, s2;
        cin>>s1>>s2;

        int a[100005], b[100005];
        int na, nb, n;
        na=0;
        nb=1;
        b[0]=0;
        n=s1.length();

        for(int i=0,j=0; i<n; ++i)
        {
            if(s1[i]==s2[i]&&j==1)
            {
                a[na++]=i+1;
                j=0;
            }
            else if(s1[i]!=s2[i]&&j==0)
            {
                a[na++]=i+1;
                j=1;
            }
        }
        for(int i=0,j=0; i<n; ++i)
        {
            if(s2[i]=='0'&&j==1)
            {
                b[nb++]=i+1;
                j=0;
            }
            else if(s2[i]!='0'&&j==0)
            {
                b[nb++]=i+1;
                j=1;
            }
        }

        if(na<nb)
        {
            for(int i=0; i<na; ++i)
                if(i<na-1)
                cout<<a[i]<<" ";
                else
                cout<<a[i];
        }
        else
        {
            for(int i=0; i<nb; ++i)
                if(i<nb-1)
                cout<<b[i]<<" ";
                else
                cout<<b[i];
        }
        cout<<endl;
    }

    return 0;
}

 类似资料: