每日一练,Broken Pad (20 分)

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

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值