列表

详情


NC212614. BrokenPad

描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.9) 解法, 执行用时: 795ms, 内存消耗: 6956K, 提交时间: 2021-03-08 16:26:13

for _ in range(int(input())):
    a=input()
    b=input()
    if '1' not in b:
        print(0)
    else:
        c=bin(int(a,2)^int(b,2))[2:]
        start=1+len(a)-len(c)
        t=0
        wz=[start]
        for i in range(len(c)):
            if c[i]==str(t):
                wz.append(i+start)
                t=1-t
        wz2=[0]
        start2=1
        t2=1
        for i in range(len(b)):
            if b[i]==str(t2):
                wz2.append(i+start2)
                t2=1-t2
        if len(wz)>len(wz2):
            print(*wz2)
        else:
            print(*wz)

C++11(clang++ 3.9) 解法, 执行用时: 67ms, 内存消耗: 1812K, 提交时间: 2020-09-27 11:09:05

#include<bits/stdc++.h>
using namespace std;
int _,n,a[100005],b[100005];
string s1,s2;
int main(){
	int i;
	for (scanf("%d",&_);_;_--){
		cin>>s1>>s2;
		n=s1.length();
		a[0]=b[1]=0;
		b[0]=1;
		for (i=0;i<n;i++)
		if ((s1[i]!=s2[i])^(a[0]&1)) a[++a[0]]=i+1;
		for (i=0;i<n;i++)
		if ((s2[i]=='0')^(b[0]&1)) b[++b[0]]=i+1;
		if (a[0]<b[0]) for (i=1;i<=a[0];i++) printf("%d ",a[i]);
		else for (i=1;i<=b[0];i++) printf("%d ",b[i]);
		printf("\n");
	}
	return 0;
}

上一题