第十七届中国计量大学程序设计竞赛(同步赛)Broken Pad(对于每一个位置记录操作的次数)

题目链接:https://ac.nowcoder.com/acm/contest/7888/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^5 ), indicating the current state of the card.

The second line contains a string b (|b| = |a|), indicating the state required by ZH.

输入

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构成的串s和t,对s串进行两种操作。问,能否变成t串。

第一种操作:对某一个位置i以及后面的位置,0变成1,1变成0
第二种操作:对s串进行清零操作,即所有的1都变成0

若能变成t串,满足最少的操作次数,输出改变的每一个位置,对于清零操作,输出的是0

分析:
对于清零操作,只能执行一次或不执行,分别计算,求最小值。

完整代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
char s[N],t[N];
int sum[N],c[N],d[N],k1,k2;
void init()
{
    memset(c,0,sizeof(c));
    memset(d,0,sizeof(d));
    k1=0,k2=0;
    sum[0]=0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%s%s",s+1,t+1);
        int len=strlen(s+1);
        int p;
        for(int i=1; i<=len; i++)
        {
            p=sum[i-1];
            if(p%2==0)///一个位置改变偶数次
            {
                if(s[i]==t[i])
                    sum[i]=sum[i-1];///sum[i]:i的位置改变的次数
                else
                    sum[i]=sum[i-1]+1,c[k1++]=i;///c数组记录改变的位置
            }
            else
            {
                if(s[i]==t[i])
                    sum[i]=sum[i-1]+1,c[k1++]=i;///本来s[i]==t[i],可是改变了奇数次,s[i]已经变化
                else
                    sum[i]=sum[i-1];
            }
        }
        for(int i=1; i<=len; i++)///执行一次清零操作
            s[i]='0';
        for(int i=1; i<=len; i++)
        {
            p=sum[i-1];
            if(p%2==0)
            {
                if(s[i]==t[i])
                    sum[i]=sum[i-1];
                else
                    sum[i]=sum[i-1]+1,d[k2++]=i;
            }
            else
            {
                if(s[i]==t[i])
                    sum[i]=sum[i-1]+1,d[k2++]=i;
                else
                    sum[i]=sum[i-1];
            }
        }
        if(k1<k2+1)
        {
            for(int i=0;i<k1;i++)
                printf("%d%c",c[i],i==k1-1?'\n':' ');
        }
        else
        {
            printf("0");
            for(int i=0;i<k2;i++)
                printf(" %d",d[i]);
            printf("\n");
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值